U  W[@@sdZddlmZmZddlZddlZddlmZmZm Z ddl m Z ddl m Z ddlmZmZmZddlmZdd d ZGd d d e jZGdddeZGdddeZdS)z Authoritative resolvers. )absolute_importdivisionN)dnserrorcommon)defer)failure)execfile nativeString_PY3)FilePath/tmp/twisted-names.serialc Cstd}td}z4tj|sFt|d}||dW5QRXW5t|Xt|d}| \}}W5QRX||krt |dpd}t|d}|d||fW5QRX|d |f}|S) ay 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. @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%dwz 0rrz%s %dz%02d) timestrftimeosumaskpathexistsopenwritereadlinesplitint)filenameserialofZ serialFileZ lastSerialZzoneIDr!9/usr/lib/python3/dist-packages/twisted/names/authority.py getSerials       r#c@sheZdZdZejejejfZej ej fZ dZ dZ ddZddZddZdd d Zdd d ZddZdS) FileAuthoritya An Authority that is loaded from a file. @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}. NcCs tj|||i|_dSN)r ResolverBase__init__loadFile_cache)selfrr!r!r"r'Os  zFileAuthority.__init__cCs ||_dSr%)__dict__)r*stater!r!r" __setstate__UszFileAuthority.__setstate__c csn||D]`}|j|jkr|jjj}|j|dD]2}|j|jkr4t j ||jt j |j pZ||ddVq4qdS)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!TauthN) type_ADDITIONAL_PROCESSING_TYPESZpayloadnamerecordsgetlowerTYPE_ADDRESS_TYPESrRRHeaderINttl)r*Zanswer authorityr:recordr2recr!r!r"_additionalRecordsYs    z FileAuthority._additionalRecordsc Csg}g}g}g}t|jdj|jdj} |j|} | r| D]} | jdk rZ| j} n| } | jt j kr||jdkr| t j || jt j | | ddn4| j|ks|t jkr| t j || jt j | | dd| jt jkrD| t j || jt j | | ddqD|s|}|||| } |r.|| n || |sp|sp| t j |jdt jt j | |jdddt|||fSt ||jdrttt |Sttt|SdS)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. rNrFr.T)maxsoaminimumexpirer3r4r5r:r6rNSappendr8r9Z ALL_RECORDSCNAMEr>extendSOArsucceedZ_isSubdomainOffailrFailureZAuthoritativeDomainErrorr DomainError)r*r2clsr0timeoutZcnamesresultsr;Z additional default_ttlZdomain_recordsr<r:ZadditionalInformationr!r!r"_lookupxs      zFileAuthority._lookup c Cs|jd|krt|jdj|jdj}|jdjdk rN|jdj}n|}tj|jdtjtj ||jdddg}|j D]T\}}|D]F}|jdk r|j} n|} |j tjkr| tj||j tj | |ddqq| |dt|ddfSttt|S)NrrTr.r!)r@r5r?rArBr:rr8rGr9r3itemsr6rDrrHrIrrJrK) r*r2rMrOZsoa_ttlrNkrr=r:r!r!r" lookupZonesB  zFileAuthority.lookupZonecCsbggg}}}|D]B}|dr||dd||dd||ddq|||fS)Nrr)rF)r*rNZansr/addresr!r!r" _cbAllRecordsszFileAuthority._cbAllRecords)N)rQ)__name__ __module__ __qualname____doc__rrEZMXrCr1AZAAAAr7r@r3r'r-r>rPrTrXr!r!r!r"r$;s   c r$c@s(eZdZdZddZddZddZdS) PySourceAuthorityzC A FileAuthority that is built up from Python source code. cCs||i}}t|||d|kr.td|i|_|dD]:}t|dtjrV||_|j|d g |dqz.PySourceAuthority.wrapRecord..r!)r*r0r!rgr" wrapRecordszPySourceAuthority.wrapRecordcCsNi}tj}dd|DD],}tt|}||}|||tdd<q|S)NcSsg|]}|dr|qS)Record_) startswith).0xr!r!r" s z:PySourceAuthority.setupConfigNamespace..rk)rr+Ziterkeysgetattrrjlen)r*rrRr<r0r r!r!r"r_s   z&PySourceAuthority.setupConfigNamespaceN)rYrZr[r\r(rjr_r!r!r!r"r^s r^c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ 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 basenameoriginZ getContent splitlines stripCommentscollapseContinuations parseLines)r*rfplinesr!r!r"r(,s   zBindAuthority.loadFilecCsdddd|DDS)z Strip comments from C{lines}. @param lines: lines to work on @type lines: iterable of L{bytes} @return: C{lines} sans comments. css2|]*}|ddkr|p(|d|dVqdS);N)find)rmar!r!r" Gsz.BindAuthority.stripComments..cSsg|] }|qSr!)strip)rmbr!r!r"roHsz/BindAuthority.stripComments..r!)r*r{r!r!r"rw>s zBindAuthority.stripCommentsc Csg}d}|D]}|dkrP|ddkr2||q||d|dd}q |ddkr|dd|d|d7<d}q |dd|7<q tddd |DS) 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(r}Nr) css|]}|VqdSr%)r)rmliner!r!r"rfsz6BindAuthority.collapseContinuations..)r~rDfilter)r*r{rdr,rr!r!r"rxNs  "z#BindAuthority.collapseContinuationscCsd}|j}i|_|D]p}|ddkr4t|d}q|ddkrJ|d}q|ddkr`tdq|ddkrvtd q||||q||_d S) zs Parse C{lines}. @param lines: lines to work on @type lines: iterable of L{bytes} i0*rs$TTLrs$ORIGINs$INCLUDEz"$INCLUDE directive not implementeds $GENERATEz#$GENERATE directive not implementedN)rur3rZstr2timeNotImplementedErrorparseRecordLine)r*r{r:rurr!r!r"ryis       zBindAuthority.parseLinescCsd|ds |d|dd}n |dd}t|d|fd}|rR|||||ntd|fdS)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} rsNr}zclass_%szRecord class %r not supported)endswithrpr)r*ownerr:r0domainrLrdatar r!r!r" addRecords  zBindAuthority.addRecordcCsjttdt|fd}|rT||}||_|j|g||dkrf||f|_nt dt|fdS)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 rdate: bytes z Record_%sNrGzRecord type %r not supported) rprr r:r3rbr5rDr@r)r*r:r0rrr<rr!r!r"class_INs  zBindAuthority.class_INc Cstr6tddtjD}tddtjD}nttj}ttj}||B}d}|}|ddkr|dd}|}n,|ds|d|kr|d}|dd}|ds|d|kr|} |}n|d} |dd}|d|kr,|d}|dd}|drtt|d}|dd}nH|drtt|d}|dd}|d|krt|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} css|]}|dVqdSasciiNencode)rmZqcr!r!r"rsz0BindAuthority.parseRecordLine..css|]}|dVqdSrr)rmZqtr!r!r"rssINr@rN) r setrZ QUERY_CLASSESvaluesZ QUERY_TYPESisdigitrrr ) r*rur:rZ queryClassesZ queryTypesZmarkersrLrrr0rr!r!r"rsZ           zBindAuthority.parseRecordLineN) rYrZr[r\r(rwrxryrrrr!r!r!r"rr%s#rr)r )r\Z __future__rrrrZ twisted.namesrrrZtwisted.internetrZtwisted.pythonrZtwisted.python.compatr r r Ztwisted.python.filepathr r#r&r$r^rrr!r!r!r"s    %K