Ϫf)AdZddlZddlZddlmZddlmZmZmZddl m Z ddl m Z m Z ddlmZddZGd d ej"ZGd d eZGd deZy)z Authoritative resolvers. N)defer)commondnserror)failure)execfile nativeString)FilePathctjd}tjd} tjj |s)t |d5}|j|dzdddtj|t |5}|jj\}}ddd|k(xrtdzxsd}t |d5}|jd||fzddd|d |fzz}|S#1swYxYw#tj|wxYw#1swY~xYw#1swYFxYw) 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%dwz 0Nrz%s %dz%02d) timestrftimeosumaskpathexistsopenwritereadlinesplitint)filenameserialof serialFile lastSerialzoneIDs 9/usr/lib/python3/dist-packages/twisted/names/authority.py getSerialr"s)"]]8 $F Aww~~h'h$ ' & '  h;:'00288: F;F" 9Vq >QF h 5 FF#3345v ) *F M ' '  ;; 55s;+DD ,D"D3#D? DDD03D<?EceZdZdZej ej ejfZejejfZ dZ dZ dZdZdZdZd dZd dZd Zy) 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 Ncrtjj||j|i|_yN)r ResolverBase__init__loadFile_cacheselfrs r!r(zFileAuthority.__init__Vs)$$T* h c||_yr&)__dict__)r,states r! __setstate__zFileAuthority.__setstate__[s  r-cy)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+s r!r)zFileAuthority.loadFile^sr-c #K||zD]}|j|jvs|jjj}|jj |j dD]^}|j|jvstj||jtj|jxs||d`yw)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}. r3TauthN) type_ADDITIONAL_PROCESSING_TYPESpayloadnamerecordsgetlowerTYPE_ADDRESS_TYPESrRRHeaderINttl)r,answer authorityrBrecordr:recs r!_additionalRecordsz FileAuthority._additionalRecordsks*y( F{{d???~~**//<<++DJJL"=Cxx4#6#66!ll #((CFFCGGNsCd s!CA%C ACc ng}g}g}g}t|jdj|jdj} |jj |j } | r/| D]q} | j | j} n| } | jtjk(rq|j |jdj k7rC|jtj|| jtj| | dnd| j|k(s|tjk(rB|jtj|| jtj| | d| jtjk(s0|jtj|| jtj| | dt|s|}|j!||| } |r|j#| n|j#| |sb|s`|jtj|jdtj$tj |jddt'j(|||fStj*||jdr;t'j,t/j0tj2|St'j,t/j0t5j6|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. rrFr5T)maxsoaminimumexpirer;r<r=rBr>rNSappendr@rA ALL_RECORDSCNAMErGextendSOArsucceed_isSubdomainOffailrFailureAuthoritativeDomainErrorr DomainError)r,r:clsr7timeoutcnamesresultsrD additional default_ttldomain_recordsrErBadditionalInformations r!_lookupzFileAuthority._lookupsg0  $((1+--txx{/A/AB ))$**,7 ( ::) **C%C;;#&&(TZZ\TXXa[=N=N=P-P$$ T6;;VRWX[[D(DCOO,CNN T6;;VRVW;;#))+MM T6;;VRVW% * %)$;$;K% !45!!"789  LL SWWcffc488A;T =='9j!AB B!!$ 4zz'//#2N2Nt2T"UVV zz'//%2C2CD2I"JKKr-c "tj|}|jdj|jk(rt |jdj |jdj }|jdj|jdj}n|}tj|jdtjtj||jddg}|jjD]\}}|D]}}|j |j} n|} |jtjk7s<|jtj||jtj| |d|j|dtj |ddfStj"t%j&tj(|S)NrrTr5r3)r domainStringrJr=rIrKrLrBr@rRrAr;itemsr>rNrrSrUrrVrX) r,r:rZr^soa_ttlr\krrFrBs r! lookupZonezFileAuthority.lookupZones% 88A;   $**, .dhhqk11488A;3E3EFKxx{*((1+//% HHQK#&&'488A;TG  **, 1Cww*!gg)xx377*LLCHHcffc3TR   NN71: &=='2r!23 3zz'//#//$*?@AAr-cggg}}}|D]M}|ds |j|dd|j|dd|j|ddO|||fS)Nrr)rQ)r,r\ansr6address r! _cbAllRecordszFileAuthority._cbAllRecordssoR3T &C1v 3q6!9% CF1I& 3q6!9%  & D#~r-r&) )__name__ __module__ __qualname____doc__rrPMXrMr8AAAAAr?rJr;r(r1r)rGrarhrnr3r-r!r$r$;se(%(IIsvvsvv#> eeSXX&N CG    > F) EB"Q%0 LL # #BqEKKM2 6 = =be D Er-cfd}|S)Nc>tj||i|fSr&)rrc)r:argkwr7s r!wrapRecordFuncz4PySourceAuthority.wrapRecord..wrapRecordFuncs#$$T*D#,<,<= =r-r3)r,r7rs ` r! wrapRecordzPySourceAuthority.wrapRecords >r-ci}tjj}|Dcgc]}|jds|c}D]4}t t|}|j |}|||t dd<6|Scc}w)NRecord_)rr/keys startswithgetattrrlen)r,rgrdxrEr7rs r!r{z&PySourceAuthority.setupConfigNamespaces{  !!#"'CQ1<< +BqC ,F3'D%A*+AfS^%& ' , Ds A<A<N)rprqrrrsr)rr{r3r-r!rxrxs E r-rxc:eZdZdZdZdZdZdZdZdZ dZ y ) BindAuthorityz An Authority that loads U{BIND zone files }. Supports only C{$ORIGIN} and C{$TTL} directives. ct|}t|jdz|_|j j d}|j |}|j|}|j|y)z} Load records from C{filename}. @param filename: file to read from @type filename: L{bytes} .TN) r r basenameorigin getContent splitlines stripCommentscollapseContinuations parseLines)r,rfpliness r!r)zBindAuthority.loadFile1sih #2;;=4#78  **40""5)**51 r-cVd|Dcgc]}|jc}DScc}w)z Strip comments from C{lines}. @param lines: lines to work on @type lines: iterable of L{bytes} @return: C{lines} sans comments. c3xK|]2}|jddk(xr|xs|d|jd4yw);N)find).0as r! z.BindAuthority.stripComments..KsA  FF4LB  $1 9.AFF4L(9 9 s8:)strip)r,rbs r!rzBindAuthority.stripCommentsBs( )./Aaggi/  /s&c rg}d}|D]}|dk(rL|jddk(r|j|.|j|d|jdd}T|jddk7r%|dxxd|d|jdzz cc<d}|dxxd|zz cc<tdd|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(rNr) c3<K|]}|jywr&)r)rlines r!rz6BindAuthority.collapseContinuations..hs8dTZZ\8s)rrNfilter)r,rrr0rs r!rz#BindAuthority.collapseContinuationsPs  )Dz99T?b(HHTNHHT"3DIIdO45E99T?b(bETD):499T?$;;;EEbETD[(E )d8a899r-cd}|j}i|_|D]j}|ddk(rtj|d}$|ddk(r|d}2|ddk(r t d|ddk(r t d |j |||l||_y ) 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)rr;rstr2timeNotImplementedErrorparseRecordLine)r,rrBrrs r!rzBindAuthority.parseLinesjs  8DAw'!ll47+aJ&aaK')*NOOaL()*OPP$$VS$7 8 r-c|jds |dz|ddz}n|dd}t|d|d}|r |||||ytd|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} rNrclass_z Record class  not supported)endswithrr)r,ownerrBr7domainrYrdatars r! addRecordzBindAuthority.addRecordsh,t$d]U3BZ/FCR[F DF3%.$ / c4 '% cWN&KL Lr-cttdt|d}|rU||}||_|jj |j gj||dk(r ||f|_yytdt|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 rNrRz Record type r) rrr rBr;rr=rNrJr)r,rBr7rrrErgs r!class_INzBindAuthority.class_INs  T(:'; >q Au}"A;&|D14NC r-c tjjDchc]}|jd}}tjjDchc]}|jd}}||z}d} |} |ddk(r|dd}|} n$|dj s|d|vr |d} |dd}|dj s|d|vr| } |} n |d} |dd}|d|vr1|d} |dd}|dj rKt |d}|dd}n7|dj r$t |d}|dd}|d|vr |d} |dd}|d} |dd} |j| |t| | t| | ycc}wcc}w)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} asciisINr@rN) r QUERY_CLASSESvaluesencode QUERY_TYPESisdigitrrr )r,rrBrqc queryClassesqt queryTypesmarkersrYrrr7rs r!rzBindAuthority.parseRecordLines695F5F5M5M5OPr '*P P36??3I3I3KLRbii(L L+ 7d?8DEa"tAwg'=GE8D 7?? Q7 2FE!WF8D 7l "q'C8DAw $q'lABx !W__ d1g,C8DAw,&1gABxAwQR uc<#5v|C?PRWXMQLs E=FN) rprqrrrsr)rrrrrrr3r-r!rr)s-"  :46M@:5Yr-r)z/tmp/twisted-names.serial)rsrrtwisted.internetr twisted.namesrrrtwisted.pythonrtwisted.python.compatrr twisted.python.filepathr r"r'r$rxrr3r-r!rs\   ",,"8,$NJF''JZ BNYMNYr-