Ϫf dZddlZddlmZddlmZddlmZmZdZ dZ Gdd e Z Gd d e Z Gd d e ZGdde ZGddej ZGddZGddej ZgdZy)z Ident protocol implementation. N)defer)basic)failurelogic eZdZdZdZdefdZy) IdentErrorz; Can't determine connection owner; reason unknown. z UNKNOWN-ERRORreturnc|jSN)identDescriptionselfs 9/usr/lib/python3/dist-packages/twisted/protocols/ident.py__str__zIdentError.__str__s$$$N)__name__ __module__ __qualname____doc__r strrrrr r s'%%rr ceZdZdZdZy)NoUserz The connection specified by the port pair is not currently in use or currently not owned by an identifiable entity. zNO-USERNrrrrr rrrrrs !rrceZdZdZdZy) InvalidPorta Either the local or foreign port was improperly specified. This should be returned if either or both of the port ids were out of range (TCP port numbers are from 1-65535), negative integers, reals or in any fashion not recognized as a non-negative integer. z INVALID-PORTNrrrrrr's&rrceZdZdZdZy) HiddenUserz The server was able to identify the user of this port, but the information was not returned at the request of the user. z HIDDEN-USERNrrrrrr2s %rrc4eZdZdZdZdZdZdZdZdZ y) IdentServera The Identification Protocol (a.k.a., "ident", a.k.a., "the Ident Protocol") provides a means to determine the identity of a user of a particular TCP connection. Given a TCP port number pair, it returns a character string which identifies the owner of that connection on the server's system. Server authors should subclass this class and override the lookup method. The default implementation returns an UNKNOWN-ERROR response for every query. c|jd}t|dk7r|jy tt|\}}t |cxkr t kr,nn)t |cxkr t krnn|j||y|jtjt||y#t$r|jYywxYw)N,) splitlen invalidQuerymapint _MIN_PORT _MAX_PORT validQuery _ebLookuprFailurer ValueError)rlineparts portOnServer portOnClients r lineReceivedzIdentServer.lineReceivedHs 3 u:?     -0e_* l ::!\>Y>OOL,?NN 6 l $!!# $sB33CCc8|jjyr ) transportloseConnectionrs rr'zIdentServer.invalidQuery\s %%'rcJ|jjj|f}|jjj|f}t j |j ||j|j||j|j||y)z Called when a valid query is received to look up and deliver the response. @param portOnServer: The server port from the query. @param portOnClient: The client port from the query. N) r6getHosthostgetPeerr maybeDeferredlookup addCallback _cbLookup addErrbackr-)rr2r3 serverAddr clientAddrs rr,zIdentServer.validQuery_s|^^++-22L@ ^^++-22L@  DKKZ@LL NNL, *T^^\< @rc>|\}}|jd||||fzy)Nz%d, %d : USERID : %s : %s)sendLine)rresultsportcportsysNameuserIds rr?zIdentServer._cbLookupms'"& 1UE7F4SSTrc|jtr"|jd|||jfzyt j ||jd||t|jfzy)Nz%d, %d : ERROR : %s)checkr rDvaluererr)rrrFrGs rr-zIdentServer._ebLookupqs] 7== $ MM/5%2OO P GGG  MM%z'--7P(QQ rct)aq Lookup user information about the specified address pair. Return value should be a two-tuple of system name and username. Acceptable values for the system name may be found online at:: U{http://www.iana.org/assignments/operating-system-names} This method may also raise any IdentError subclass (or IdentError itself) to indicate user information will not be provided for the given query. A Deferred may also be returned. @param serverAddress: A two-tuple representing the server endpoint of the address being queried. The first element is a string holding a dotted-quad IP address. The second element is an integer representing the port. @param clientAddress: Like I{serverAddress}, but represents the client endpoint of the address being queried. r )r serverAddress clientAddresss rr=zIdentServer.lookupzs .lrN) rrrrr4r'r,r?r-r=rrrr!r!;s' (( AUrr!cjeZdZdZdZ ddlmZefdZ[dZ d Z d Z d Z d Z y#e$rd dZYwxYw)ProcServerMixinzDImplements lookup() to grab entries for responses from /proc/net/tcpLINUXr)getpwuidc||dSNrrruidrUs r getUsernamezProcServerMixin.getUsernamesC=# #rNctr rOrXs rrZzProcServerMixin.getUsernames , rc#Ktd5}|j|D]}|j dddy#1swYyxYww)Nz /proc/net/tcp)openreadlinestrip)rfLs rentrieszProcServerMixin.entriessG / " a JJL ggi    s A*A AA Acdjtttjdtj dt |dS)N.4Bz=L)joinr(rstructunpackpackr))rhexstrs rdottedQuadFromHexStringz'ProcServerMixin.dottedQuadFromHexStrings9xx V]]4T3vr?)KL M  rcl|jd\}}|j|}t|d}||fS)N:rf)r%rlr))rpackedaddrports r unpackAddresszProcServerMixin.unpackAddresss9\\#& d++D14}Tzrc|jj}|j|d\}}|j|d\}}t|d}||f||f|fS)Nrr$)r_r%rrr))rr0r1 localAddr localPort remoteAddr remotePortrYs r parseLinezProcServerMixin.parseLinesl ""$#11%(; 9!%!3!3E!H!= J%(m9% J'?DDrc|jD]G}|j|\}}}||k(s|d|dk(s*|j|j|fcSt )Nr)rbry SYSTEM_NAMErZr)rrPrQentrurwrYs rr=zProcServerMixin.lookupsk<<> AC)-)< &Iz3]*y|}Q?O/O(($*:*:3*?@@ A hrr )rrrrr{pwdrUrZ ImportErrorrbrlrrryr=rrrrSrSsONK  ,4 $     E;  s & 22rSc6eZdZeeeefZdZdZ dZ dZ dZ y) IdentClientcg|_yr )queriesrs r__init__zIdentClient.__init__s  rc|jjtj||ft |jdkDr|jddS|j d||fz|jddS)zK Lookup user information about the specified address pair. rr%d, %d)rappendrDeferredr&rD)rr2r3s rr=zIdentClient.lookupss U^^-|\JK t|| q <<#A& & h, !==>||B""rc6|jstjd|y|jjd\}}}|j |||jr7|j d|jdd|jddfzyy)NzUnexpected server response: rrrr$)rrmsgpop parseResponserD)rr0d_s rr4zIdentClient.lineReceiveds|| GG24(; <ll&&q)GAq!   q$ '|| h$,,q/!*P)QQRrcn|jD]}|djt|!g|_yrW)rerrbackr )rreasonqs rconnectionLostzIdentClient.connectionLosts1 -A aDLLF+ , - rc|jdd}t|dk7r|jt|yt t j |\}}}|dk(rT|jD]*}|j|k(s|j||y|jt|y|j||fy)Nrnr$ERROR) r%r&rr r(rr_ errorTypesr callback)rdeferredr0r1portstypeaddInfoets rrzIdentClient.parseResponses 3" u:?   Z- .#&syy%#8 E4w//B**g5 ((D2  D!12!!4/2rN) rrrr rrrrrr=r4rrrrrrrs*fk:>J #S 3rr)r rrrr!rrS)rrhtwisted.internetrtwisted.protocolsrtwisted.pythonrrr*r+ Exceptionr rrrLineOnlyReceiverr!rSr__all__rrrrs "#'  %%!Z!&*&%%V%((Vr//d,3%((,3^ r