o ¯b+Aã@sœdZddlZddlZddlmZddlmZmZmZddl m Z ddl m Z m Z ddlmZdd d „ZGd d „d ejƒZGd d„deƒZGdd„deƒZdS)z Authoritative resolvers. éN)Údefer)ÚcommonÚdnsÚerror)Úfailure)ÚexecfileÚ nativeString)ÚFilePathú/tmp/twisted-names.serialc Cst d¡}t d¡}z)tj |¡s-t|dƒ}| |d¡Wdƒn1s(wYWt |¡nt |¡wt|ƒ}| ¡  ¡\}}Wdƒn1sQwY||kr`t |ƒdpad}t|dƒ}| d||f¡Wdƒn1s{wY|d |f}|S) aÛ Return a monotonically increasing (across program runs) integer. State is stored in the given file. If it does not exist, it is created with rw-/---/--- permissions. This manipulates process-global state by calling C{os.umask()}, so it isn't thread-safe. @param filename: Path to a file that is used to store the state across program runs. @type filename: L{str} @return: a monotonically increasing number @rtype: L{str} z%Y%m%déÚwz 0Nérz%s %dz%02d) ÚtimeÚstrftimeÚosÚumaskÚpathÚexistsÚopenÚwriteÚreadlineÚsplitÚint)ÚfilenameÚserialÚoÚfÚ serialFileÚ lastSerialÚzoneID©r ú9/usr/lib/python3/dist-packages/twisted/names/authority.pyÚ getSerials$    ÿ€ ÿ ÿr"c@speZdZdZejejejfZej ej fZ dZ dZ dd„Zdd„Zdd„Zd d „Zdd d „Zddd„Zdd„ZdS)Ú FileAuthoritya­ An Authority that is loaded from a file. This is an abstract class that implements record search logic. To create a functional resolver, subclass it and override the L{loadFile} method. @ivar _ADDITIONAL_PROCESSING_TYPES: Record types for which additional processing will be done. @ivar _ADDRESS_TYPES: Record types which are useful for inclusion in the additional section generated during additional processing. @ivar soa: A 2-tuple containing the SOA domain name as a L{bytes} and a L{dns.Record_SOA}. @ivar records: A mapping of domains (as lowercased L{bytes}) to records. @type records: L{dict} with L{bytes} keys NcCs tj |¡| |¡i|_dS©N)rÚ ResolverBaseÚ__init__ÚloadFileÚ_cache©Úselfrr r r!r&Vs   zFileAuthority.__init__cCs ||_dSr$)Ú__dict__)r*Ústater r r!Ú __setstate__[s zFileAuthority.__setstate__cCsdS)ab Load DNS records from a file. This method populates the I{soa} and I{records} attributes. It must be overridden in a subclass. It is called once from the initializer. @param filename: The I{filename} parameter that was passed to the initilizer. @returns: L{None} -- the return value is ignored Nr r)r r r!r'^szFileAuthority.loadFilec csp||D]0}|j|jvr5|jjj}|j | ¡d¡D]}|j|jvr4t j ||jt j |j p.||ddVqqdS)aW Find locally known information that could be useful to the consumer of the response and construct appropriate records to include in the I{additional} section of that response. Essentially, implement RFC 1034 section 4.3.2 step 6. @param answer: A L{list} of the records which will be included in the I{answer} section of the response. @param authority: A L{list} of the records which will be included in the I{authority} section of the response. @param ttl: The default TTL for records for which this is not otherwise specified. @return: A generator of L{dns.RRHeader} instances for inclusion in the I{additional} section. These instances represent extra information about the records in C{answer} and C{authority}. r T©ÚauthN) ÚtypeÚ_ADDITIONAL_PROCESSING_TYPESÚpayloadÚnameÚrecordsÚgetÚlowerÚTYPEÚ_ADDRESS_TYPESrÚRRHeaderÚINÚttl)r*ÚanswerÚ authorityr;Úrecordr3Úrecr r r!Ú_additionalRecordsks€    ÿ€ûz FileAuthority._additionalRecordsc Cs´g}g}g}g}t|jdj|jdjƒ} |j | ¡¡} | r»| D]_} | jdur,| j} n| } | jt j krP| ¡|jd ¡krP|  t j || jt j | | dd¡n| j|ksZ|t jkrj|  t j || jt j | | dd¡| jt jkr€|  t j || jt j | | dd¡q!|s…|}| ||| ¡} |r”| | ¡n| | ¡|s³|s³|  t j |jdt jt j | |jddd¡t |||f¡St  ||jd¡rÏt t t  |¡¡¡St t t |¡¡¡S)a  Determine a response to a particular DNS query. @param name: The name which is being queried and for which to lookup a response. @type name: L{bytes} @param cls: The class which is being queried. Only I{IN} is implemented here and this value is presently disregarded. @type cls: L{int} @param type: The type of records being queried. See the types defined in L{twisted.names.dns}. @type type: L{int} @param timeout: All processing is done locally and a result is available immediately, so the timeout value is ignored. @return: A L{Deferred} that fires with a L{tuple} of three sets of response records (to comprise the I{answer}, I{authority}, and I{additional} sections of a DNS response) or with a L{Failure} if there is a problem processing the query. r NrFr.T)ÚmaxÚsoaÚminimumÚexpirer4r5r6r;r7rÚNSÚappendr9r:Ú ALL_RECORDSÚCNAMEr@ÚextendÚSOArÚsucceedÚ_isSubdomainOfÚfailrÚFailureÚAuthoritativeDomainErrorrÚ DomainError)r*r3Úclsr0ÚtimeoutÚcnamesÚresultsr=Ú additionalÚ default_ttlÚdomain_recordsr>r;ÚadditionalInformationr r r!Ú_lookup‰sT "ÿÿ ÿ€ÿ  ÿÿzFileAuthority._lookupé c Cst |¡}|jd ¡| ¡krt|jdj|jdjƒ}|jdjdur,|jdj}n|}tj|jdtj tj ||jdddg}|j   ¡D]*\}}|D]#}|jdurX|j} n|} |j tj krp| tj||j tj | |dd¡qMqG| |d¡t |ddf¡St t t |¡¡¡S)Nrr Tr.r )rÚ domainStringrBr6rArCrDr;r9rJr:r4Úitemsr7rFrrKrMrrNrP) r*r3rRrVÚsoa_ttlrTÚkÚrr?r;r r r!Ú lookupZoneâs0 ÿÿ  ÿ€ú zFileAuthority.lookupZonecCsbggg}}}|D]!}|dr+| |dd¡| |dd¡| |dd¡q |||fS)Nrr é)rI)r*rTÚansr/ÚaddÚresr r r!Ú _cbAllRecordsþs€ zFileAuthority._cbAllRecordsr$)rZ)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrHÚMXrEr1ÚAÚAAAAr8rBr4r&r-r'r@rYr`rer r r r!r#;s   Y r#c@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚPySourceAuthorityzC A FileAuthority that is built up from Python source code. cCs|| ¡i}}t|||ƒd|vrtd|ƒ‚i|_|dD]}t|dtjƒr+||_|j |d  ¡g¡  |d¡qdS)NÚzonezNo zone defined in r r) ÚsetupConfigNamespacerÚ ValueErrorr4Ú isinstancerÚ Record_SOArBÚ setdefaultr6rF)r*rÚgÚlÚrrr r r!r' s   "ýzPySourceAuthority.loadFilecs‡fdd„}|S)Ncst |¡ˆ|i|¤ŽfSr$)rr[)r3ÚargÚkw©r0r r!ÚwrapRecordFuncsz4PySourceAuthority.wrapRecord..wrapRecordFuncr )r*r0rzr ryr!Ú wrapRecords zPySourceAuthority.wrapRecordcCsNi}tj ¡}dd„|DƒD]}tt|ƒ}| |¡}|||tdƒd…<q|S)NcSsg|] }| d¡r|‘qS)ÚRecord_)Ú startswith)Ú.0Úxr r r!Ú "sz:PySourceAuthority.setupConfigNamespace..r|)rr+ÚkeysÚgetattrr{Úlen)r*r_r\r>r0rr r r!ros   z&PySourceAuthority.setupConfigNamespaceN)rfrgrhrir'r{ror r r r!rms  rmc@sHeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)Ú BindAuthorityz™ An Authority that loads U{BIND zone files }. Supports only C{$ORIGIN} and C{$TTL} directives. cCsJt|ƒ}t| ¡dƒ|_| ¡ d¡}| |¡}| |¡}| |¡dS)z} Load records from C{filename}. @param filename: file to read from @type filename: L{bytes} ó.TN) r rÚbasenameÚoriginÚ getContentÚ splitlinesÚ stripCommentsÚcollapseContinuationsÚ parseLines)r*rÚfpÚlinesr r r!r'1s   zBindAuthority.loadFilecCsdd„dd„|DƒDƒS)z« Strip comments from C{lines}. @param lines: lines to work on @type lines: iterable of L{bytes} @return: C{lines} sans comments. css4|]}| d¡dkr |p|d| d¡…VqdS)ó;éÿÿÿÿN)Úfind)r~Úar r r!Ú Ks €"ÿ ÿz.BindAuthority.stripComments..cSsg|]}| ¡‘qSr )Ústrip)r~Úbr r r!r€Msz/BindAuthority.stripComments..r )r*rŽr r r!rŠBs þzBindAuthority.stripCommentsc Cs°g}d}|D]G}|dkr(| d¡dkr| |¡q| |d| d¡…¡d}q| d¡dkrC|dd|d| d¡…7<d}q|dd|7<qtddd „|DƒƒS) zÄ Transform multiline statements into single lines. @param lines: lines to work on @type lines: iterable of L{bytes} @return: iterable of continuous lines ró(rNr ó)ó css|]}| ¡VqdSr$)r)r~Úliner r r!r“hs€z6BindAuthority.collapseContinuations..)r‘rFÚfilter)r*rŽrur,r™r r r!r‹Ps  "z#BindAuthority.collapseContinuationscCsŒd}|j}i|_|D]6}|ddkrt |d¡}q |ddkr%|d}q |ddkr/tdƒ‚|ddkr9td ƒ‚| |||¡q ||_d S) zs Parse C{lines}. @param lines: lines to work on @type lines: iterable of L{bytes} i0*rs$TTLr s$ORIGINs$INCLUDEz"$INCLUDE directive not implementeds $GENERATEz#$GENERATE directive not implementedN)r‡r4rÚstr2timeÚNotImplementedErrorÚparseRecordLine)r*rŽr;r‡r™r r r!rŒjs      zBindAuthority.parseLinescCsd| d¡s|d|dd…}n|dd…}t|d|›dƒ}|r*|||||ƒdStd|›dƒ‚)a  Add a record to our authority. Expand domain with origin if necessary. @param owner: origin? @type owner: L{bytes} @param ttl: time to live for the record @type ttl: L{int} @param domain: the domain for which the record is to be added @type domain: L{bytes} @param type: record type @type type: L{str} @param cls: record class @type cls: L{str} @param rdata: record data @type rdata: L{list} of L{bytes} r…NrÚclass_z Record class ú not supported)Úendswithr‚rœ)r*Úownerr;r0ÚdomainrQÚrdatarr r r!Ú addRecord…s  zBindAuthority.addRecordcCsnttdt|ƒ›dƒ}|r-||Ž}||_|j | ¡g¡ |¡|dkr+||f|_dSdSt dt|ƒ›dƒ‚)a> Simulate a class IN and recurse into the actual class. @param ttl: time to live for the record @type ttl: L{int} @param type: record type @type type: str @param domain: the domain @type domain: bytes @param rdata: @type rdata: bytes r|NrJz Record type rŸ) r‚rrr;r4rsr6rFrBrœ)r*r;r0r¢r£r>r_r r r!Úclass_IN¥sÿÿzBindAuthority.class_INc Csvdd„tj ¡Dƒ}dd„tj ¡Dƒ}||B}d}|}|ddkr+|dd…}|}n|d ¡sA|d|vrA|d}|dd…}|d ¡sM|d|vrR|} |}n |d} |dd…}|d|vr|d}|dd…}|d ¡r~t|dƒ}|dd…}n"|d ¡r¡t|dƒ}|dd…}|d|vr¡|d}|dd…}|d} |dd…} | ||t| ƒ| t|ƒ| ¡dS) aƒ Parse a C{line} from a zone file respecting C{origin} and C{ttl}. Add resulting records to authority. @param origin: starting point for the zone @type origin: L{bytes} @param ttl: time to live for the record @type ttl: L{int} @param line: zone file line to parse; split by word @type line: L{list} of L{bytes} cSóh|]}| d¡’qS©Úascii©Úencode)r~Úqcr r r!Ú Ñóz0BindAuthority.parseRecordLine..cSr¦r§r©)r~Úqtr r r!r¬Òr­sINró@r N)rÚ QUERY_CLASSESÚvaluesÚ QUERY_TYPESÚisdigitrr¤r) r*r‡r;r™Ú queryClassesÚ queryTypesÚmarkersrQr¡r¢r0r£r r r!rÂs@         €       zBindAuthority.parseRecordLineN) rfrgrhrir'rŠr‹rŒr¤r¥rr r r r!r„)s r„)r )rirrÚtwisted.internetrÚ twisted.namesrrrÚtwisted.pythonrÚtwisted.python.compatrrÚtwisted.python.filepathr r"r%r#rmr„r r r r!Ús    'N!