3 Ó•íWZœã@sNdZddlmZddlZddlZddlZddlmZddlZ ddl Z ddl Z ddl Z ddl Z ddlZ ddlZ ddlZ ddlZ ddlZ ddlmZmZejdkZGdd „d e jjƒZGd d „d eƒZGd d „d eƒZGdd„deƒZGdd„deƒZGdd„deƒZ de j!j"dedddfdd„Z#de j!j"dedddfdd„Z$eddfdd„Z%dS)z DNS Zones.é)Ú generatorsN)ÚBytesIOé)Ú string_typesÚ text_typeéc@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 dfdd„Z d d „Z d d „Z d d„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „ZeZd!d"„Zd#d$„ZdCd&d'„ZdDd(d)„Zd*d+„Zejjd%fd,d-„Z ejjd%fd.d/„Z!ejjfd0d1„Z"d2d3„Z#ejjfd4d5„Z$ejjfd6d7„Z%ejj&ejjfd8d9„Z'ejj&ejjfd:d;„Z(dEd=d>„Z)dFd?d@„Z*dAdB„Z+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_rrset“s  zZone.get_rrsetccs~t|tƒrtjj|ƒ}t|tƒr,tjj|ƒ}xL|jƒD]@\}}x6|D].}|tjjksh|j|krD|j|krD||fVqDWq6WdS)a¦Return 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Ú iteritemsÚANYr<r=)rr<r=rr6Úrdsr r rÚiterate_rdatasetsµs      zZone.iterate_rdatasetsccs�t|tƒrtjj|ƒ}t|tƒr,tjj|ƒ}x^|jƒD]R\}}xH|D]@}|tjjksh|j|krD|j|krDx|D]}||j |fVqnWqDWq6WdS)a¢Return 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=rr6rIÚrdatar r rÚiterate_rdatasÌs       zZone.iterate_rdatasNc Cs8t|tƒrt|dƒ}d}nd}t|ddƒ}|dkr6d}|dkrPtjj|ƒ}d}n"t|tƒrf|j|ƒ}n |}|jƒ}z°|rŽt|j ƒƒ}|j ƒn|j ƒ}xˆ|D]€} || j | |j |d�} t| tƒrÌ| j|ƒ} n | } | jƒ} y|j| ƒ|j|ƒWqœtk �r|j| ƒ|j|ƒYqœXqœWWd|�r2|jƒXdS) 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 ÚwbTFÚencodingNzutf-8Ú )rr)rrÚopenÚgetattrÚosÚlinesepÚencodeÚdecodeÚlistr-Úsortr.Úto_textrrÚwriteÚ TypeErrorÚclose) rÚfÚsortedrÚnlÚ want_closeZfile_encZnl_bÚnamesÚnÚlZl_br r rÚto_fileäsD             z Zone.to_filecCs*tƒ}|j||||ƒ|jƒ}|jƒ|S)a·Return 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 )rrdÚgetvaluer\)rr^rr_Z temp_bufferZ return_valuer r rrY$s z Zone.to_textcCsJ|jrtjj}n|j}|j|tjjƒdkr.t‚|j|tjj ƒdkrFt ‚dS)zÐDo 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) rrrÚemptyrr?r:ZSOArZNSr)rrr r rÚ check_origin9s zZone.check_origin)F)F)TTN)TTN),r r r r rr6ZNoder4Ú __slots__Ú rdataclassÚINrr!r"r%r'r)r*r+r.r-r0r/r1rGr2r3r7r8r9r:ZNONEr;r?r@rBrErFrHrJrMrdrYrgr r r rr=sJ      # !(" @ rc@sHeZdZdZeddfdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)Ú _MasterReadera¼Read 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|tƒrtjj|ƒ}||_||_||_d|_|j|_ ||||d�|_ g|_ d|_ ||_ ||_dS)Nr)r)rrrrrÚtokÚcurrent_originrrKÚ last_nameÚzoneÚ saved_stateÚ current_fileÚ allow_includerg)rrlrrrÚ zone_factoryrrrgr r rrfs  z_MasterReader.__init__cCsx|jjƒ}|jƒrPqWdS)N)rlr2Ú is_eol_or_eof)rÚtokenr r rÚ _eat_lineus z_MasterReader._eat_linec "Cs¢|jdkrt‚|jjdd�}|jƒs:tjj|j|jƒ|_ n"|jjƒ}|j ƒrPdS|jj |ƒ|j }|j |j jƒs||jƒdS|jr�|j|j jƒ}|jjƒ}|jƒsªtjj‚y,tjj|jƒ}|jjƒ}|jƒsÔtjj‚Wntjjk rô|j}YnXy.tjj|jƒ}|jjƒ}|jƒ�s"tjj‚Wn>tjjk �rFtjj‚Yntk �rb|j j}YnX||j jk�r~tjjdƒ‚ytjj|jƒ}Wntjjd|jƒ‚YnX|j jj|ƒ}|dk�rÞ|j jƒ}||j j|<ytjj|||j|jdƒ}Wnltjjk �r,tjƒdd…\}} | ‚Yn<tjƒdd…\}} tjjdt|ƒt| ƒfƒ‚YnX|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'Fézcaught exception %s: %s)$rmrrlr2Z is_whitespacerrrr(rnrtÚungetr$rorrvrÚ is_identifierÚ exceptionÚ SyntaxErrorrKÚBadTTLriÚ Exceptionrr:rr4rLÚsysÚexc_infoÚstrÚchoose_relativityr=r;Úadd) rrurrKrr<rbÚrdÚtyÚvar=rIr r rÚ_rr_line{sx               z_MasterReader._rr_linec Csðtjdƒ}tjdƒ}tjdƒ}|j|ƒ}|rJ|jƒ\}}}} } |dkrJd}|j|ƒ} | rz| jƒ\}}}|dkrrd}d} d} |j|ƒ} | r´|jƒ\}}}} |dkr¤d}|jƒd} d} |p¾| p¾| sÔd}d}d}d} d} | dkrâtƒ‚|||| | fS) Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$Úú+rÚdrw)ÚreÚcompileÚmatchÚgroupsÚNotImplementedError) rZsideZ is_generate1Z is_generate2Z is_generate3Zg1ÚmodZsignÚoffsetÚwidthÚbaseZg2Zg3r r rÚ _parse_modifyÇs>        z_MasterReader._parse_modifyc!7Cs|jdkrt‚|jjƒ}y2tjj|jƒ\}}}|jjƒ}|jƒsHtj j ‚Wntj j ‚YnXy$|j}|jjƒ}|jƒs‚tj j ‚Wntj j ‚YnXy,tj j|jƒ}|jjƒ}|jƒsÄtj j ‚Wntj j k rä|j }YnXy.tj j|jƒ}|jjƒ}|jƒ�stj j ‚Wn>tj j k �r6tj j ‚Yntk �rR|jj}YnX||jjk�rntj j dƒ‚y.tjj|jƒ}|jjƒ}|jƒ�sštj j ‚Wn(tk �rÄtj j d|jƒ‚YnXy |j} Wntj j ‚YnX|j|ƒ\} } } } }|j| ƒ\}}}}}�xòt||d|ƒD�]Ü}| dk�r@|t| ƒ}n| dk�rV|t| ƒ}|dk�rn|t|ƒ}n|dk�r„|t|ƒ}t|ƒjt| ƒƒ}t|ƒjt|ƒƒ}|jd| |ƒ}| jd||ƒ}tjj||jƒ|_|j}|j|jjƒ�sü|jƒdS|j�r|j|jjƒ}|jjj|ƒ}|dk�r@|jjƒ}||jj|<ytj j||||jdƒ}Wnltj j k �rŒt!j"ƒdd …\}}|‚Yn<t!j"ƒdd …\}}tj j d t|ƒt|ƒfƒ‚YnX|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:r“ÚrangeÚintr€ÚzfillÚreplacerrnr$rrvrrr4rLr~rr�r=r;r‚)!rruÚstartÚstopÚstepZlhsrKrr<ZrhsZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseÚiZlindexÚrindexZlzfindexZrzfindexrrLrbrƒr„r…r=rIr r rÚ_generate_lineðs®                            z_MasterReader._generate_linecCs’�y�x|jjddƒ}|jƒrh|jdk r2|jjƒt|jƒdkrb|jjdƒ\|_|_|_ |_|_ qP�nž|j ƒrvq�n�|j ƒrŽ|jj ƒq�nx|jddk�r|jjƒ}|dkrê|jjƒ}|jƒsÎtjjdƒ‚tj j|jƒ|_ |jj ƒq|dk�r$|jjƒ|_|jj ƒ|jjdk�r|j|j_q|d k�rÜ|j�rÜ|jjƒ}|j}|jjƒ}|jƒ�rxtjj|j|jƒ}|jj ƒn|jƒ�s�tjjd ƒ‚n|j}|jj|j|j|j |j|j fƒt|d ƒ|_tjj|j|ƒ|_||_q|d k�rð|jƒqtjjd |dƒ‚q|jj |ƒ|j!ƒqWWnZtjjk �rz}z8|jj"ƒ\}}|dk�rTd}tjjd|||fƒ‚WYdd}~XnX|j#�rŽ|jj#ƒdS)zÈRead 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 $INCLUDEÚrz $GENERATEzUnknown master file directive 'ú'z syntax errorz %s:%d: %séÿÿÿÿ)$rlr2Zis_eofrqr\rArpÚpoprmrnrKZis_eolZ is_commentZget_eolr(Úupperryrrzr{rZget_namerorrrrrtÚappendrQÚ tokenizerÚ Tokenizerržrxr†Úwhererg)rruÚcÚfilenameZ new_originZdetailZ line_numberr r rÚreades€                          "z_MasterReader.readN) r r r r rrrvr†r“ržr«r 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)rr¦r§rkr«ro) ÚtextrrrrsrªrrrgrlÚreaderr r rr·s" rc Cslt}d} t||ƒr.|dkr|}t|| ƒ}d} n|dkr:d}d} zt||||||||ƒ} Wd| rf|jƒX| S)a³Read 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]rrrrsrªrrrgZstr_typeZoptsr`Úzr r rÚ from_fileãs     r¯c CsÚd}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 | ƒq¢WqPWq W|rÖ|jƒ|S)a’Convert 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_ttlrKr�r‚rg) Zxfrrsrrgr®r rrrCZznodeZzrdsrƒr r rÚfrom_xfrs,         r°)r)&r Z __future__rr~rŠrSÚiorZ dns.exceptionrZdns.nameZdns.nodeZdns.rdataclassZ dns.rdatatypeZ dns.rdataZ dns.rrsetZ dns.tokenizerZdns.ttlZ dns.grangeZ_compatrrÚ version_infor,rzZ DNSExceptionrrrrÚobjectrrkrirjrr¯r°r r r rÚsH   o*3