
                            d Z ddlmZ ddlZddlZddlZddl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mZmZmZ  G d de	j0                  j2                        Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z de	jB                  jD                  dedddfdZ#de	jB                  jD                  dedddfdZ$eddfdZ%y)z
DNS Zones.    )
generatorsN)BytesIO   )string_types	text_typePY3c                       e Zd ZdZy)BadZonezThe DNS zone is malformed.N__name__
__module____qualname____doc__     lib/third_party/dns/zone.pyr
   r
   )   s    $r   r
   c                       e Zd ZdZy)NoSOAz)The DNS zone has no SOA RR at its origin.Nr   r   r   r   r   r   .   s    3r   r   c                       e Zd ZdZy)NoNSz+The DNS zone has no NS RRset at its origin.Nr   r   r   r   r   r   3   s    5r   r   c                       e Zd ZdZy)UnknownOriginz!The DNS zone's origin is unknown.Nr   r   r   r   r   r   8   s    +r   r   c                      e Zd ZdZej
                  j                  Zg dZej                  j                  dfdZd Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZeZd Zd Zd#dZd#dZd Zej<                  j>                  dfdZ ej<                  j>                  dfdZ!ej<                  j>                  fdZ"d Z#ej<                  j>                  fdZ$ej<                  j>                  fdZ%ej<                  jL                  ej<                  j>                  fdZ'ej<                  jL                  ej<                  j>                  fdZ(d$d Z)d$d!Z*d" Z+y)%ZoneaZ  A DNS zone.

    A Zone is a mapping from names to nodes.  The zone object may be
    treated like a Python dictionary, e.g. zone[name] will retrieve
    the node associated with that name.  The I{name} may be a
    dns.name.Name object, or it may be a string.  In the either case,
    if the name is relative it is treated as relative to the origin of
    the zone.

    @ivar rdclass: The zone's rdata class; the default is class IN.
    @type rdclass: int
    @ivar origin: The origin of the zone.
    @type origin: dns.name.Name object
    @ivar nodes: A dictionary mapping the names of nodes in the zone to the
    nodes themselves.
    @type nodes: dict
    @ivar relativize: should names in the zone be relativized?
    @type relativize: bool
    @cvar node_factory: the factory used to create a new node
    @type node_factory: class or callable
    )rdclassoriginnodes
relativizeTc                 4   |zt        |t              r t        j                  j	                  |      }n/t        |t        j                  j
                        st        d      |j                         st        d      || _        || _	        i | _
        || _        y)zInitialize a zone object.

        @param origin: The origin of the zone.
        @type origin: dns.name.Name object
        @param rdclass: The zone's rdata class; the default is class IN.
        @type rdclass: intNz2origin parameter must be convertible to a DNS namez)origin parameter must be an absolute name)
isinstancer   dnsname	from_textName
ValueErroris_absoluter   r   r   r   )selfr   r   r   s       r   __init__zZone.__init__Y   s     &,/++F36  ", - -%%' !LMM
$r   c                     t        |t              sy| j                  |j                  k7  s2| j                  |j                  k7  s| j                  |j                  k7  ryy)ziTwo zones are equal if they have the same origin, class, and
        nodes.
        @rtype: bool
        FT)r    r   r   r   r   r'   others     r   __eq__zZone.__eq__n   sG     %&<<5==(;;%,,&::$r   c                 &    | j                  |       S )z6Are two zones not equal?
        @rtype: bool
        )r,   r*   s     r   __ne__zZone.__ne__|   s    
 ;;u%%%r   c                    t        |t              r!t        j                  j	                  |d       }n/t        |t        j                  j
                        st        d      |j                         rM|j                  | j                        st        d      | j                  r|j                  | j                        }|S )Nz0name parameter must be convertible to a DNS namez5name parameter must be a subdomain of the zone origin)r    r   r!   r"   r#   r$   KeyErrorr&   is_subdomainr   r   r'   r"   s     r   _validate_namezZone._validate_name   s    dL)88%%dD1DD#((--0MNN$$T[[1KM Mt{{3r   c                 B    | j                  |      }| j                  |   S Nr3   r   r'   keys     r   __getitem__zZone.__getitem__   s     !!#&zz#r   c                 D    | j                  |      }|| j                  |<   y r5   r6   )r'   r8   values      r   __setitem__zZone.__setitem__   s    !!#&

3r   c                 @    | j                  |      }| j                  |= y r5   r6   r7   s     r   __delitem__zZone.__delitem__   s    !!#&JJsOr   c                 6    | j                   j                         S r5   )r   __iter__r'   s    r   r@   zZone.__iter__   s    zz""$$r   c                 v    t         r| j                  j                         S | j                  j                         S r5   )r   r   keysiterkeysrA   s    r   rD   zZone.iterkeys   s)    ::??$$::&&((r   c                 6    | j                   j                         S r5   )r   rC   rA   s    r   rC   z	Zone.keys   s    zz  r   c                 v    t         r| j                  j                         S | j                  j                         S r5   )r   r   values
itervaluesrA   s    r   rH   zZone.itervalues   s+    ::$$&&::((**r   c                 6    | j                   j                         S r5   )r   rG   rA   s    r   rG   zZone.values   s    zz  ""r   c                 6    | j                   j                         S r5   )r   itemsrA   s    r   rK   z
Zone.items   s    zz!!r   c                 Z    | j                  |      }| j                  j                  |      S r5   )r3   r   getr7   s     r   rM   zZone.get   s%    !!#&zz~~c""r   c                     || j                   v S r5   )r   r*   s     r   __contains__zZone.__contains__   s    

""r   Fc                     | j                  |      }| j                  j                  |      }|'|st        | j	                         }|| j                  |<   |S )ar  Find a node in the zone, possibly creating it.

        @param name: the name of the node to find
        @type name: dns.name.Name object or string
        @param create: should the node be created if it doesn't exist?
        @type create: bool
        @raises KeyError: the name is not known and create was not specified.
        @rtype: dns.node.Node object
        )r3   r   rM   r0   node_factoryr'   r"   createnodes       r   	find_nodezZone.find_node   sT     ""4(zz~~d#<$$&D#DJJtr   c                 P    	 | j                  ||      }|S # t        $ r d}Y |S w xY w)a  Get a node in the zone, possibly creating it.

        This method is like L{find_node}, except it returns None instead
        of raising an exception if the node does not exist and creation
        has not been requested.

        @param name: the name of the node to find
        @type name: dns.name.Name object or string
        @param create: should the node be created if it doesn't exist?
        @type create: bool
        @rtype: dns.node.Node object or None
        N)rU   r0   rR   s       r   get_nodezZone.get_node   s9    	>>$/D   	D	s    %%c                 ^    | j                  |      }|| j                  v r| j                  |= yy)zhDelete the specified node if it exists.

        It is not an error if the node does not exist.
        Nr6   r2   s     r   delete_nodezZone.delete_node   s1     ""4(4::

4  r   c                 @   | j                  |      }t        |t              rt        j                  j                  |      }t        |t              rt        j                  j                  |      }| j                  ||      }|j                  | j                  |||      S )a  Look for rdata with the specified name and type in the zone,
        and return an rdataset 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.

        The rdataset returned is not a copy; changes to it will change
        the zone.

        KeyError is raised if the name or type are not found.
        Use L{get_rdataset} if you want to have None returned 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
        @param create: should the node and rdataset be created if they do not
        exist?
        @type create: bool
        @raises KeyError: the node or rdata could not be found
        @rtype: dns.rdataset.Rdataset object
        )	r3   r    r   r!   	rdatatyper#   rU   find_rdatasetr   )r'   r"   rdtypecoversrS   rT   s         r   r\   zZone.find_rdataset   s{    8 ""4(fl+]],,V4Ffl+]],,V4F~~dF+!!$,,GGr   c                 T    	 | j                  ||||      }|S # t        $ r d}Y |S w xY w)a  Look for rdata with the specified name and type in the zone,
        and return an rdataset 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.

        The rdataset returned is not a copy; changes to it will change
        the zone.

        None is returned if the name or type are not found.
        Use L{find_rdataset} 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
        @param create: should the node and rdataset be created if they do not
        exist?
        @type create: bool
        @rtype: dns.rdataset.Rdataset object or None
        N)r\   r0   )r'   r"   r]   r^   rS   rdatasets         r   get_rdatasetzZone.get_rdataset  s?    6	))$GH   	H	s    ''c                    | j                  |      }t        |t              rt        j                  j                  |      }t        |t              rt        j                  j                  |      }| j                  |      }|>|j                  | j                  ||       t        |      dk(  r| j                  |       yyy)a  Delete the rdataset matching I{rdtype} and I{covers}, if it
        exists at the node specified by I{name}.

        The I{name}, I{rdtype}, and I{covers} parameters may be
        strings, in which case they will be converted to their proper
        type.

        It is not an error if the node does not exist, or if there is no
        matching rdataset at the node.

        If the node has no rdatasets after the deletion, it will itself
        be deleted.

        @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
        Nr   )r3   r    r   r!   r[   r#   rW   delete_rdatasetr   lenrY   )r'   r"   r]   r^   rT   s        r   rc   zZone.delete_rdataset4  s    , ""4(fl+]],,V4Ffl+]],,V4F}}T"  vv>4yA~  &  r   c                     |j                   | j                   k7  rt        d      | j                  |d      }|j                  |       y)a:  Replace an rdataset at name.

        It is not an error if there is no rdataset matching I{replacement}.

        Ownership of the I{replacement} object is transferred to the zone;
        in other words, this method does not store a copy of I{replacement}
        at the node, it stores I{replacement} itself.

        If the I{name} node does not exist, it is created.

        @param name: the owner name
        @type name: DNS.name.Name object or string
        @param replacement: the replacement rdataset
        @type replacement: dns.rdataset.Rdataset
        z#replacement.rdclass != zone.rdclassTN)r   r%   rU   replace_rdataset)r'   r"   replacementrT   s       r   rf   zZone.replace_rdatasetU  s@    " $,,.BCC~~dD)k*r   c                    | j                  |      }t        |t              rt        j                  j                  |      }t        |t              rt        j                  j                  |      }| j                  |   j                  | j                  ||      }t        j                  j                  || j                  ||      }|j                  |       |S )a  Look 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{find_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.

        KeyError is raised if the name or type are not found.
        Use L{get_rrset} if you want to have None returned 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
        @raises KeyError: the node or rdata could not be found
        @rtype: dns.rrset.RRset object
        )r3   r    r   r!   r[   r#   r   r\   r   rrsetRRsetupdate)r'   r"   r]   r^   r`   ri   s         r   
find_rrsetzZone.find_rrsetk  s    < ""4(fl+]],,V4Ffl+]],,V4F::d#11$,,O		dllFFCXr   c                 R    	 | j                  |||      }|S # t        $ r d}Y |S w xY w)aJ  Look 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)rl   r0   )r'   r"   r]   r^   ri   s        r   	get_rrsetzZone.get_rrset  s;    8	OOD&&9E   	E	s    &&c              #     K   t        |t              rt        j                  j	                  |      }t        |t              rt        j                  j	                  |      }| j                         D ]O  \  }}|D ]E  }|t        j                  j                  k(  s |j                  |k(  s0|j                  |k(  s@||f G Q yw)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)	r    r   r!   r[   r#   	iteritemsANYr]   r^   )r'   r]   r^   r"   rT   rdss         r   iterate_rdatasetszZone.iterate_rdatasets  s      fl+]],,V4Ffl+]],,V4F NN,LT4S]]...JJ&(SZZ6-A+%  -s   B&C)C9Cc              #     K   t        |t              rt        j                  j	                  |      }t        |t              rt        j                  j	                  |      }| j                         D ]a  \  }}|D ]W  }|t        j                  j                  k(  s |j                  |k(  s0|j                  |k(  s@|D ]  }||j                  |f  Y c yw)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)
r    r   r!   r[   r#   rp   rq   r]   r^   ttl)r'   r]   r^   r"   rT   rr   rdatas          r   iterate_rdataszZone.iterate_rdatas  s      fl+]],,V4Ffl+]],,V4F NN,LT4S]]...JJ&(SZZ6-A!$#SWWe44 "%  -s   B&C)C9CNc                 J   t        |t              rt        |d      }d}nd}t        |dd      }|d}|"t        j
                  j                  |      }d}n4t        |t              r|j                  |      }n|}|j                         }	 |r*t        | j                               }|j                          n| j                         }|D ]z  }	| |	   j                  |	| j                  |      }
t        |
t              r|
j                  |      }n|
}|
j                         }
	 |j                  |       |j                  |       | 	 |r|j#                          yy# t         $ r% |j                  |
       |j                  |       Y w xY w# |r|j#                          w w xY w)	a  Write 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encodingNzutf-8
)r   r   )r    r   opengetattroslinesepencodedecodelistrC   sortrD   to_textr   r   write	TypeErrorclose)r'   fsortedr   nl
want_closefile_encnl_bnamesnll_bs               r   to_filezZone.to_file  sm   $ a&QAJJ 1j$/H:::$$X.DBL)99X&DDB	TYY[)

GOOAdkk/9 $ ;a+((8,CC
A GGCLGGDM   	 	 !  GGAJGGBK  	 s1   BF #"EF +F
F 	F

F F"c                     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
        )r   r   getvaluer   )r'   r   r   r   temp_bufferreturn_values         r   r   zZone.to_text$  s=     i[&*b9"++-r   c                 *   | j                   rt        j                  j                  }n| j                  }| j                  |t        j                  j                        t        | j                  |t        j                  j                        t        y)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)r   r!   r"   emptyr   ra   r[   SOAr   NSr   r2   s     r   check_originzZone.check_origin9  sj     ??88>>D;;DT3==#4#45=KT3==#3#34<J =r   )F)TTN),r   r   r   r   r!   rT   NoderQ   	__slots__
rdataclassINr(   r,   r.   r3   r9   r<   r>   r@   rD   rC   rH   rG   rK   rp   rM   rO   rU   rW   rY   r[   NONEr\   ra   rc   rf   rl   rn   rq   rs   rw   r   r   r   r   r   r   r   r   =   sR   , 88==L<I'*~~'8'8T %*& %)!+#" I##((! 251C1C""HH 140B0B!B 47==3E3E 'B+, /2mm.@.@ &P .1]]-?-?  D (+}}'8'8!$!3!3&. %(MM$5$5!mm0050>@*r   r   c                   <    e Zd ZdZeddfdZd Zd Zd Zd Z	d	 Z
y
)_MasterReadera(  Read a DNS master file

    @ivar tok: The tokenizer
    @type tok: dns.tokenizer.Tokenizer object
    @ivar last_ttl: The last seen explicit TTL for an RR
    @type last_ttl: int
    @ivar last_ttl_known: Has last TTL been detected
    @type last_ttl_known: bool
    @ivar default_ttl: The default TTL from a $TTL directive or SOA RR
    @type default_ttl: int
    @ivar default_ttl_known: Has default TTL been detected
    @type default_ttl_known: bool
    @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,
    last_ttl, last_ttl_known, default_ttl, default_ttl_known) 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
    FTc                 >   t        |t              rt        j                  j	                  |      }|| _        || _        || _        d| _        d| _	        d| _
        d| _        | j                  | _         ||||      | _        g | _        d | _        || _        || _        y )Nr   Fr   )r    r   r!   r"   r#   tokcurrent_originr   last_ttllast_ttl_knowndefault_ttldefault_ttl_known	last_namezonesaved_statecurrent_fileallow_includer   )r'   r   r   r   r   zone_factoryr   r   s           r   r(   z_MasterReader.__init__l  s    fl+XX''/F$$#!&,, ZH	 *(r   c                 \    	 | j                   j                         }|j                         ry ,r5   )r   rM   is_eol_or_eof)r'   tokens     r   	_eat_linez_MasterReader._eat_line~  s(    HHLLNE""$ r   c           	      |   | j                   t        | j                  j                  d      }|j	                         s:t
        j                  j                  |j                  | j                         | _	        nF| j                  j                         }|j                         ry| j                  j                  |       | j                  }|j                  | j                  j                        s| j                          y| j                   r%|j!                  | j                  j                        }| j                  j                         }|j#                         st
        j$                  j&                  	 t
        j(                  j                  |j                        }|| _        d| _        | j                  j                         }|j#                         st
        j$                  j&                  	 	 t
        j4                  j                  |j                        }| j                  j                         }|j#                         st
        j$                  j&                  	 || j                  j8                  k7  rt
        j$                  j'                  d      	 t
        j:                  j                  |j                        }| j                  j<                  j                  |      }|3| j                  j?                         }|| j                  j<                  |<   	 t
        j@                  j                  ||| j                  | j                   d      }| j0                  sPtK        |t
        jL                  jN                  jP                  jP                        r|jR                  | _        d| _        |jU                  | j                  j                  | j                          |jW                         }
|jY                  |||
d      }|j[                  ||       y# t
        j(                  j.                  $ r` | j,                  s+| j0                  st
        j$                  j'                  d      | j0                  r| j2                  }n| j*                  }Y w xY w# t
        j$                  j&                  $ r t
        j$                  j&                  t6        $ r | j                  j8                  }Y w xY w#  t
        j$                  j'                  d|j                  z        xY w# t
        j$                  j&                  $ r tC        jD                         dd \  }}	|	 tC        jD                         dd \  }}	t
        j$                  j'                  d	jG                  tI        |      tI        |	                  xY w)
z(Process one line from a DNS master file.NT)want_leadingMissing default TTL valueRR class is not zone's classunknown rdatatype '%s'F   zcaught exception {}: {}).r   r   r   rM   is_whitespacer!   r"   r#   r;   r   r   ungetr1   r   r   r   r   is_identifier	exceptionSyntaxErrorru   r   r   BadTTLr   r   r   	Exceptionr   r[   r   rQ   rv   sysexc_infoformatstrr    rdtypesrq   r   minimumchoose_relativityr^   r\   add)r'   r   r"   ru   r   r]   r   rdtyvar^   rr   s               r   _rr_linez_MasterReader._rr_line  s7    &$/""$ XX//T002DN HHLLNE""$HHNN5!~~  !1!12NN????499#3#34D""$--+++	$''##EKK0CDM"&DHHLLNE&&(mm/// )	(nn..u{{;GHHLLNE&&(mm/// ) dii'''--++,JKK	8]],,U[[9F IIOO%9		&&(A$%DIIOOD!	D$$Wfdhh%)%8%8%AB  %%*R9L9L9P9P*Q  "zzD%)D"
TYY--t?oogvvt<Cq ww~~ 	$''4+A+Amm//0KLL%%&&mm	$ }}(( 	,--+++ 	(ii''G	(	8--++(5;;68 8 }}(( 	||~bq)HRH	D ||~bq)HR--++)00R#b'BD DsA   7A;P 4A-R )S5 7T& A9RRAS21S25.T#&BV;c                    t        j                  d      }t        j                  d      }t        j                  d      }|j                  |      }|r|j                         \  }}}}	}
|dk(  rd}|j                  |      }|r|j                         \  }}}|dk(  rd}d}	d}
|j                  |      }|r1|j                         \  }}}}	|dk(  rd}|j                         d   }	d}
|s|s|s
d}d}d}d}	d}

dk7  r
t	               	|
fS )	Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$ +r   dr   )recompilematchgroupsNotImplementedError)r'   sideis_generate1is_generate2is_generate3g1modsignoffsetwidthbaseg2g3s                r   _parse_modifyz_MasterReader._parse_modify  s+    zz"GHzz"=>zz"CD %-/YY[*Cvudrz% "		CvrzED%')yy{$CvurzIIKNEDbBCDFED3;%''D&%--r   c           
      N   | j                   t        | j                  j                         }	 t        j
                  j                  |j                        \  }}}| j                  j                         }|j                         st        j                  j                  	 	 |j                  }| j                  j                         }|j                         st        j                  j                  	 	 t        j                  j                  |j                        }|| _        d| _        | j                  j                         }|j                         st        j                  j                  	 	 t        j"                  j                  |j                        }| j                  j                         }|j                         st        j                  j                  	 || j&                  j(                  k7  rt        j                  j                  d      	 t        j*                  j                  |j                        }| j                  j                         }|j                         st        j                  j                  	 	 |j                  }	| j-                  |      \  }
}}}}| j-                  |	      \  }}}}}t/        ||dz   |      D ]R  }|dk(  r|t1        |      z   }n|dk(  r|t1        |      z
  }|dk(  r|t1        |      z
  }n|dk(  r|t1        |      z   }t3              j5                  t1        |            }t3              j5                  t1        |            }|j7                  d	|
z  |      }|	j7                  d	|z  |      }t        j8                  j                  || j                         | _        | j:                  }|j=                  | j&                  j>                        s| jA                           y| jB                  r%|jC                  | j&                  j>                        }| j&                  jD                  j                  |      }|3| j&                  jG                         }|| j&                  jD                  |<   	 t        jH                  j                  |||| j                   d
      }|jO                  | j&                  j>                  | jB                         |jQ                         }|jS                  |||d      } | jU                  ||       U y#  t        j                  j                  xY w#  t        j                  j                  xY w# t        j                  j                  $ r` | j                  s+| j                  st        j                  j                  d      | j                  r| j                   }n| j                  }Y rw xY w# t        j                  j                  $ r t        j                  j                  t$        $ r | j&                  j(                  }Y _w xY w# t$        $ r- t        j                  j                  d|j                  z        w xY w#  t        j                  j                  xY w# t        j                  j                  $ r tK        jL                         dd \  }}| tK        jL                         dd \  }}t        j                  j                  dt3        |      dt3        |            xY w)zRProcess one line containing the GENERATE statement from a DNS
        master file.NTr   r   r   r   r   -z$%sFr   zcaught exception z: )+r   r   r   rM   r!   granger#   r;   r   r   r   ru   r   r   r   r   r   r   r   r   r   r[   r   rangeintr   zfillreplacer"   r   r1   r   r   r   r   rQ   rv   r   r   r   r^   r\   r   )!r'   r   startstopsteplhsru   r   r]   rhslmodlsignloffsetlwidthlbasermodrsignroffsetrwidthrbaseilindexrindexlzfindexrzfindexr"   rv   r   r   r   r   r^   rr   s!                                    r   _generate_linez_MasterReader._generate_line  s    &	, #

 4 4U[[ AE4HHLLNE&&(mm/// )	,++CHHLLNE&&(mm/// )	$''##EKK0CDM"&DHHLLNE&&(mm/// )	(nn..u{{;GHHLLNE&&(mm/// ) dii'''--++,JKK	9]],,U[[9FHHLLNE&&(mm/// )	,++C /3.@.@.E+eWfe.2.@.@.E+eWfeudQh-A }S\)$S\)}S\)$S\)6{((V5H6{((V5H;;v9DKK$:E XX//d6I6IJDN>>D$$TYY%5%56 tyy'7'78		##D)AyII**,()		%DYY((&%)-)<)<eE    !1!14??CYY[F//'664@CGGBg .s	,--+++	,--+++ ww~~ 	$''4+A+Amm//0KLL%%&&mm	$ }}(( 	,--+++ 	(ii''G	(  	9--++,D,1KK-8 9 9	9	,--+++L ==,, <<>"1-RD <<>"1-Rmm//14R#b'1C D Dsg   A1T' !AU 3A;U% 0A-W% A-Y Y: -Z'UU"%A9W"!W"%AX>=X>6Y7:ZB\$c           
      	   	 	 | j                   j                  dd      }|j                         r| j                  | j                  j	                          t        | j                        dkD  rM| j                  j                  d      \  | _         | _        | _	        | _        | _
        | _        | _        | _        n|j                         r|j                         r| j                   j!                          |j"                  d   dk(  r|j"                  j%                         }|dk(  r| j                   j                         }|j'                         st(        j*                  j-                  d      t(        j.                  j1                  |j"                        | _        d| _        | j                   j!                          nU|dk(  rm| j                   j3                         | _        | j                   j!                          | j4                  j6                   | j                  | j4                  _        n|d	k(  r| j8                  r| j                   j                         }|j"                  }| j                   j                         }|j'                         rOt(        j:                  j1                  |j"                  | j                        }| j                   j!                          n;|j=                         st(        j*                  j-                  d
      | j                  }| j                  j?                  | j                   | j                  | j                  | j                  | j                  | j                  | j                  | j                  f       tA        |d      | _        t(        jB                  jE                  | j                  |      | _         || _        n;|dk(  r| jG                          n%t(        j*                  j-                  d|z   dz         | j                   jI                  |       | jK                          E	 | jN                  r| j4                  jO                          yy# t(        j*                  j,                  $ rK}| j                   jM                         \  }}|d}t(        j*                  j-                  d|||fz        d}~ww xY w)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
        TNr   $z$TTLzbad $TTLz$ORIGINz$INCLUDEzbad origin in $INCLUDErz	$GENERATEzUnknown master file directive ''zsyntax errorz	%s:%d: %s)(r   rM   is_eofr   r   rd   r   popr   r   r   r   r   r   is_eol
is_commentget_eolr;   upperr   r!   r   r   ru   r#   get_namer   r   r   r"   r   appendr|   	tokenizer	Tokenizerr   r   r   wherer   )r'   r   cfilename
new_origindetailline_numbers          r   readz_MasterReader.read  s   K	?T40<<>((4))//14++,q0 483C3C3G3G3K1,*,)/ \\^%%'HH$$&[[^t+))+AG| $$224"%--";";J"GG+.77+<+<U[[+I(15.((*j.2hh.?.?.A+((*99++3/3/B/BDII,k)d.@.@ $#(;; $ ..0 # 2 25;;373F3F!H ' !HH,,.!&!4!4!6"%--";"; 8#: : *.)<)<J((//151D1D15151B1B15151D1D151A1A151G1G1I J -13,?)#&==#:#:4;L;L;C$E.8+l*++-!mm77=ACGI Iu%I  | II""$  }}(( 	?&*hhnn&6#X{~'--++xf==? ?		?s   QQ1 1SASSN)r   r   r   r   r   r(   r   r   r   r   r  r   r   r   r   r   J  s8    @ GK$4)$Xt'.RzxV%r   r   TFc           	          |d}t         j                  j                  | |      }t        |||||||      }	|	j	                          |	j
                  S )ar  Build 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 '<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
    z<string>)r   r   )r!   r  r  r   r  r   )
textr   r   r   r   r  r   r   r   readers
             r   r#   r#     sU    D 
--
!
!$
1C3\)6(46F KKM;;r   c           
          t         }t        rd}	nd}	t        | |      r|| }t        | |	      } d}
n|d}d}
	 t	        | |||||||      }|
r| j                          |S # |
r| j                          w w xY w)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 '<file>', 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
    r  rUTz<file>F)r   r   r    r|   r#   r   )r   r   r   r   r   r  r   r   str_typeoptsr   zs               r   	from_filer    s    > H
!XHDM
H
a*l|= GGIH GGI s   A A3c                    d}| D ]3  }|L|r|j                   }n|j                  d   j                  }|j                  d   j                  } ||||      }|j                  D ]  }|j                  j                  |j                        }	|	s)|j                         }	|	|j                  |j                  <   |	j                  |j                  |j                  |j                  d      }
|
j                  |j                         |D ]/  }|j                  |j                   |       |
j                  |       1  6 |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   r   T)r   answerr"   r   r   rM   rQ   r\   r]   r^   
update_ttlru   r   r   r   )xfrr   r   r   r  r  r   r   ri   znodezrdsr   s               r   from_xfrr%  @  s   " 	A9!))hhqk))GVWDAXXEGGKK

+E(&+

#&&u}}ell',||T;DOOEII&$$QXXz:   & 	Hr   )&r   
__future__r   r   r   r~   ior   dns.exceptionr!   dns.namedns.nodedns.rdataclassdns.rdatatype	dns.rdatadns.rdtypes.ANY.SOA	dns.rrsetdns.tokenizerdns.ttl
dns.grange_compatr   r   r   r   DNSExceptionr
   r   r   r   objectr   r   r   r   r#   r  r%  r   r   r   <module>r6     s   $  ! 
 	 	             1 1%cmm(( %
4G 4
67 6
,G ,
J6 JZO%F O%d  ):):D4!)X cnn&7&7D4 t5p  $4 'r   