3 ӕWZ@sNdZddlmZddlZddlZddlZddlmZddlZ ddl Z ddl Z ddl Z ddl Z ddlZ ddlZ ddlZ ddlZ ddlZ ddlmZmZejdkZGdd d e jjZGd d d eZGd d d eZGdddeZGdddeZGdddeZ de j!j"dedddfddZ#de j!j"dedddfddZ$eddfddZ%dS)z DNS Zones.) generatorsN)BytesIO) string_types text_typec@seZdZdZdS)BadZonezThe DNS zone is malformed.N)__name__ __module__ __qualname____doc__r r /usr/lib/python3.6/zone.pyr)src@seZdZdZdS)NoSOAz)The DNS zone has no SOA RR at its origin.N)r r r r r r r rr.src@seZdZdZdS)NoNSz+The DNS zone has no NS RRset at its origin.N)r r r r r r r rr3src@seZdZdZdS) UnknownOriginz!The DNS zone's origin is unknown.N)r r r r r r r rr8src@sjeZdZdZejjZddddgZej j dfddZ d d Z d d Z d dZddZddZddZddZddZddZddZddZdd ZeZd!d"Zd#d$ZdCd&d'ZdDd(d)Zd*d+Zejjd%fd,d-Z ejjd%fd.d/Z!ejjfd0d1Z"d2d3Z#ejjfd4d5Z$ejjfd6d7Z%ejj&ejjfd8d9Z'ejj&ejjfd:d;Z(dEd=d>Z)dFd?d@Z*dAdBZ+drCr r r find_rrsetks      zZone.find_rrsetc Cs0y|j|||}Wntk r*d}YnX|S)aJLook for rdata with the specified name and type in the zone, and return an RRset encapsulating it. The I{name}, I{rdtype}, and I{covers} parameters may be strings, in which case they will be converted to their proper type. This method is less efficient than the similar L{get_rdataset} because it creates an RRset instead of returning the matching rdataset. It may be more convenient for some uses since it returns an object which binds the owner name to the rdata. This method may not be used to create new nodes or rdatasets; use L{find_rdataset} instead. None is returned if the name or type are not found. Use L{find_rrset} if you want to have KeyError raised instead. @param name: the owner name to look for @type name: DNS.name.Name object or string @param rdtype: the rdata type desired @type rdtype: int or string @param covers: the covered type (defaults to None) @type covers: int or string @rtype: dns.rrset.RRset object N)rEr#)rrr<r=rCr r r get_rrsets  zZone.get_rrsetccs~t|trtjj|}t|tr,tjj|}xL|jD]@\}}x6|D].}|tjjksh|j|krD|j|krD||fVqDWq6WdS)aReturn a generator which yields (name, rdataset) tuples for all rdatasets in the zone which have the specified I{rdtype} and I{covers}. If I{rdtype} is dns.rdatatype.ANY, the default, then all rdatasets will be matched. @param rdtype: int or string @type rdtype: int or string @param covers: the covered type (defaults to None) @type covers: int or string N) rrrr:r iteritemsANYr<r=)rr<r=rr6rdsr r riterate_rdatasetss      zZone.iterate_rdatasetsccst|trtjj|}t|tr,tjj|}x^|jD]R\}}xH|D]@}|tjjksh|j|krD|j|krDx|D]}||j |fVqnWqDWq6WdS)aReturn a generator which yields (name, ttl, rdata) tuples for all rdatas in the zone which have the specified I{rdtype} and I{covers}. If I{rdtype} is dns.rdatatype.ANY, the default, then all rdatas will be matched. @param rdtype: int or string @type rdtype: int or string @param covers: the covered type (defaults to None) @type covers: int or string N) rrrr:rrGrHr<r=ttl)rr<r=rr6rIrdatar r riterate_rdatass       zZone.iterate_rdatasNc Cs8t|trt|d}d}nd}t|dd}|dkr6d}|dkrPtjj|}d}n"t|trf|j|}n |}|j}z|rt|j }|j n|j }x|D]} || j | |j |d} t| tr| j|} n | } | j} y|j| |j|Wqtk r|j| |j|YqXqWWd|r2|jXdS) aWrite a zone to a file. @param f: file or string. If I{f} is a string, it is treated as the name of a file to open. @param sorted: if True, the file will be written with the names sorted in DNSSEC order from least to greatest. Otherwise the names will be written in whatever order they happen to have in the zone's dictionary. @param relativize: if True, domain names in the output will be relativized to the zone's origin (if possible). @type relativize: bool @param nl: The end of line string. If not specified, the output will use the platform's native end-of-line marker (i.e. LF on POSIX, CRLF on Windows, CR on Macintosh). @type nl: string or None wbTFencodingNzutf-8 )rr)rropengetattroslinesepencodedecodelistr-sortr.to_textrrwrite TypeErrorclose) rfsortedrnl want_closeZfile_encZnl_bnamesnlZl_br r rto_filesD             z Zone.to_filecCs*t}|j|||||j}|j|S)aReturn a zone's text as though it were written to a file. @param sorted: if True, the file will be written with the names sorted in DNSSEC order from least to greatest. Otherwise the names will be written in whatever order they happen to have in the zone's dictionary. @param relativize: if True, domain names in the output will be relativized to the zone's origin (if possible). @type relativize: bool @param nl: The end of line string. If not specified, the output will use the platform's native end-of-line marker (i.e. LF on POSIX, CRLF on Windows, CR on Macintosh). @type nl: string or None )rrdgetvaluer\)rr^rr_Z temp_bufferZ return_valuer r rrY$s z Zone.to_textcCsJ|jrtjj}n|j}|j|tjjdkr.t|j|tjj dkrFt dS)zDo some simple checking of the zone's origin. @raises dns.zone.NoSOA: there is no SOA RR @raises dns.zone.NoNS: there is no NS RRset @raises KeyError: there is no origin node N) rrremptyrr?r:ZSOArZNSr)rrr r r check_origin9s zZone.check_origin)F)F)TTN)TTN),r r r r rr6ZNoder4 __slots__ rdataclassINrr!r"r%r'r)r*r+r.r-r0r/r1rGr2r3r7r8r9r:ZNONEr;r?r@rBrErFrHrJrMrdrYrgr r r rr=sJ      # !(" @ rc@sHeZdZdZeddfddZddZdd Zd d Zd d Z ddZ dS) _MasterReaderaRead a DNS master file @ivar tok: The tokenizer @type tok: dns.tokenizer.Tokenizer object @ivar ttl: The default TTL @type ttl: int @ivar last_name: The last name read @type last_name: dns.name.Name object @ivar current_origin: The current origin @type current_origin: dns.name.Name object @ivar relativize: should names in the zone be relativized? @type relativize: bool @ivar zone: the zone @type zone: dns.zone.Zone object @ivar saved_state: saved reader state (used when processing $INCLUDE) @type saved_state: list of (tokenizer, current_origin, last_name, file) tuples. @ivar current_file: the file object of the $INCLUDed file being parsed (None if no $INCLUDE is active). @ivar allow_include: is $INCLUDE allowed? @type allow_include: bool @ivar check_origin: should sanity checks of the origin node be done? The default is True. @type check_origin: bool FTcCsbt|trtjj|}||_||_||_d|_|j|_ ||||d|_ g|_ d|_ ||_ ||_dS)Nr)r)rrrrrtokcurrent_originrrK last_namezone saved_state current_file allow_includerg)rrlrrr zone_factoryrrrgr r rrfs  z_MasterReader.__init__cCsx|jj}|jrPqWdS)N)rlr2 is_eol_or_eof)rtokenr r r _eat_lineus z_MasterReader._eat_linec "Cs|jdkrt|jjdd}|js:tjj|j|j|_ n"|jj}|j rPdS|jj ||j }|j |j js||jdS|jr|j|j j}|jj}|jstjjy,tjj|j}|jj}|jstjjWntjjk r|j}YnXy.tjj|j}|jj}|js"tjjWn>tjjk rFtjjYntk rb|j j}YnX||j jkr~tjjdytjj|j}Wntjjd|jYnX|j jj|}|dkr|j j}||j j|<ytjj|||j|jd}Wnltjjk r,tjdd\}} | Yn<tjdd\}} tjjdt|t| fYnX|j |j j|j|j!} |j"||| d} | j#||dS) z(Process one line from a DNS master file.NT)Z want_leadingzRR class is not zone's classzunknown rdatatype '%s'Fzcaught exception %s: %s)$rmrrlr2Z is_whitespacerrrr(rnrtungetr$rorrvr is_identifier exception SyntaxErrorrKBadTTLri Exceptionrr:rr4rLsysexc_infostrchoose_relativityr=r;add) rrurrKrr<rbrdtyvar=rIr r r_rr_line{sx               z_MasterReader._rr_linec Cstjd}tjd}tjd}|j|}|rJ|j\}}}} } |dkrJd}|j|} | rz| j\}}}|dkrrd}d} d} |j|} | r|j\}}}} |dkrd}|jd} d} |p| p| sd}d}d}d} d} | dkrt|||| | fS) Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$+rdrw)recompilematchgroupsNotImplementedError) rZsideZ is_generate1Z is_generate2Z is_generate3Zg1modZsignoffsetwidthbaseZg2Zg3r r r _parse_modifys>        z_MasterReader._parse_modifyc!7Cs|jdkrt|jj}y2tjj|j\}}}|jj}|jsHtj j Wntj j YnXy$|j}|jj}|jstj j Wntj j YnXy,tj j|j}|jj}|jstj j Wntj j k r|j }YnXy.tj j|j}|jj}|jstj j Wn>tj j k r6tj j Yntk rR|jj}YnX||jjkrntj j dy.tjj|j}|jj}|jstj j Wn(tk rtj j d|jYnXy |j} Wntj j YnX|j|\} } } } }|j| \}}}}}xt||d|D]}| dkr@|t| }n| dkrV|t| }|dkrn|t|}n|dkr|t|}t|jt| }t|jt|}|jd| |}| jd||}tjj||j|_|j}|j|jjs|jdS|jr|j|jj}|jjj|}|dkr@|jj}||jj|<ytj j||||jd}Wnltj j k rt!j"dd \}}|Yn<t!j"dd \}}tj j d t|t|fYnX|j#|jj|j|j$}|j%|||d } | j&||q"WdS) zRProcess one line containing the GENERATE statement from a DNS master file.NzRR class is not zone's classzunknown rdatatype '%s'rr-z$%sFrwzcaught exception %s: %sT)'rmrrlr2rZgrangerr(ryrzr{rKr|rir}rorr:rrangeintrzfillreplacerrnr$rrvrrr4rLr~rrr=r;r)!rrustartstopstepZlhsrKrr<ZrhsZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseiZlindexrindexZlzfindexZrzfindexrrLrbrrrr=rIr r r_generate_lines                            z_MasterReader._generate_linecCsyx|jjdd}|jrh|jdk r2|jjt|jdkrb|jjd\|_|_|_ |_|_ qPn|j rvqn|j r|jj qnx|jddkr|jj}|dkr|jj}|jstjjdtj j|j|_ |jj q|dkr$|jj|_|jj |jjdkr|j|j_q|d kr|jr|jj}|j}|jj}|jrxtjj|j|j}|jj n|jstjjd n|j}|jj|j|j|j |j|j ft|d |_tjj|j||_||_q|d kr|jqtjjd |dq|jj ||j!qWWnZtjjk rz}z8|jj"\}}|dkrTd}tjjd|||fWYdd}~XnX|j#r|jj#dS)zRead a DNS master 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$z$TTLzbad $TTLz$ORIGINz$INCLUDEzbad origin in $INCLUDErz $GENERATEzUnknown master file directive ''z syntax errorz %s:%d: %s)$rlr2Zis_eofrqr\rArppoprmrnrKZis_eolZ is_commentZget_eolr(upperryrrzr{rZget_namerorrrrrtappendrQ tokenizer Tokenizerrrxrwhererg)rrucfilenameZ new_originZdetailZ line_numberr r rreades                          "z_MasterReader.readN) r r r r rrrvrrrrr r r rrkJsL)urkTFc Cs>|dkr d}tjj||}t|||||||d} | j| jS)arBuild a zone object from a master file format string. @param text: the master file format input @type text: string. @param origin: The origin of the zone; if not specified, the first $ORIGIN statement in the master file will determine the origin of the zone. @type origin: dns.name.Name object or string @param rdclass: The zone's rdata class; the default is class IN. @type rdclass: int @param relativize: should names be relativized? The default is True @type relativize: bool @param zone_factory: The zone factory to use @type zone_factory: function returning a Zone @param filename: The filename to emit when describing where an error occurred; the default is ''. @type filename: string @param allow_include: is $INCLUDE allowed? @type allow_include: bool @param check_origin: should sanity checks of the origin node be done? The default is True. @type check_origin: bool @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 @rtype: dns.zone.Zone object Nz)rrrg)rrrrkrro) textrrrrsrrrrgrlreaderr r rrs" rc Cslt}d} t||r.|dkr|}t|| }d} n|dkr:d}d} zt||||||||} Wd| rf|jX| S)aRead a master file and build a zone object. @param f: file or string. If I{f} is a string, it is treated as the name of a file to open. @param origin: The origin of the zone; if not specified, the first $ORIGIN statement in the master file will determine the origin of the zone. @type origin: dns.name.Name object or string @param rdclass: The zone's rdata class; the default is class IN. @type rdclass: int @param relativize: should names be relativized? The default is True @type relativize: bool @param zone_factory: The zone factory to use @type zone_factory: function returning a Zone @param filename: The filename to emit when describing where an error occurred; the default is '', or the value of I{f} if I{f} is a string. @type filename: string @param allow_include: is $INCLUDE allowed? @type allow_include: bool @param check_origin: should sanity checks of the origin node be done? The default is True. @type check_origin: bool @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 @rtype: dns.zone.Zone object ZrUNTzF)rrrQrr\) r]rrrrsrrrrgZstr_typeZoptsr`zr r r from_files     rc Csd}x|D]}|dkrH|r"|j}n |jdj}|jdj}||||d}x||jD]r}|jj|j} | sz|j} | |j|j<| j|j|j|j d} | j |j x$|D]} | j |j|| j | qWqPWq W|r|j|S)aConvert the output of a zone transfer generator into a zone object. @param xfr: The xfr generator @type xfr: generator of dns.message.Message objects @param relativize: should names be relativized? The default is True. It is essential that the relativize setting matches the one specified to dns.query.xfr(). @type relativize: bool @param check_origin: should sanity checks of the origin node be done? The default is True. @type check_origin: bool @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 @rtype: dns.zone.Zone object Nr)rT)rZanswerrrrr2r4r;r<r=Z update_ttlrKrrrg) ZxfrrsrrgrrrrrCZznodeZzrdsrr r rfrom_xfrs,         r)r)&r Z __future__rr~rrSiorZ dns.exceptionrZdns.nameZdns.nodeZdns.rdataclassZ dns.rdatatypeZ dns.rdataZ dns.rrsetZ dns.tokenizerZdns.ttlZ dns.grangeZ_compatrr version_infor,rzZ DNSExceptionrrrrobjectrrkrirjrrrr r r rsH   o*3