o b@sdZddlZddlmZddlmZddlmZmZdZ dZ Gdd d e Z Gd d d e Z Gd d d e ZGddde ZGdddejZGdddZGdddejZgdZdS)z Ident protocol implementation. N)defer)basic)failurelogic@s"eZdZdZdZdefddZdS) IdentErrorz; Can't determine connection owner; reason unknown. z UNKNOWN-ERRORreturncCs|jSN)identDescriptionselfr 9/usr/lib/python3/dist-packages/twisted/protocols/ident.py__str__zIdentError.__str__N)__name__ __module__ __qualname____doc__r strrr r r rrsrc@eZdZdZdZdS)NoUserz The connection specified by the port pair is not currently in use or currently not owned by an identifiable entity. zNO-USERNrrrrr r r r rrrc@r) 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-PORTNrr r r rr'src@r) 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-USERNrr r r rr2rrc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) 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. cCs|d}t|dkr|dSz tt|\}}Wnty(|YdSwt|kr3tkrInnt|kr?tkrInn|||dS| t t ||dS)N,) splitlen invalidQuerymapint ValueError _MIN_PORT _MAX_PORT validQuery _ebLookuprFailurer)r lineparts portOnServer portOnClientr r r lineReceivedHs    zIdentServer.lineReceivedcCs|jdSr ) transportloseConnectionr r r rr!\szIdentServer.invalidQuerycCsL|jj|f}|jj|f}t|j|||j|| |j ||dS)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) r/getHosthostgetPeerr maybeDeferredlookup addCallback _cbLookup addErrbackr()r r,r- serverAddr clientAddrr r rr'_s zIdentServer.validQuerycCs"|\}}|d||||fdS)Nz%d, %d : USERID : %s : %s)sendLine)r resultsportcportsysNameuserIdr r rr7mszIdentServer._cbLookupcCsL|tr|d|||jfdSt||d||t|jfdS)Nz%d, %d : ERROR : %s)checkrr;valuererr)r rr=r>r r rr(qs  zIdentServer._ebLookupcCt)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 clientAddressr r rr5zszIdentServer.lookupN) rrrrr.r!r'r7r(r5r r r rr;s  rc@sxeZdZdZdZzddlmZefddZ[Wney%dddZYnwd d Z d d Z d dZ ddZ ddZ dS)ProcServerMixinzDImplements lookup() to grab entries for responses from /proc/net/tcpLINUXr)getpwuidcCs ||dSNrr r uidrJr r r getUsernames zProcServerMixin.getUsernameNcCrDr rErLr r rrNrccsLtd}||D]}|Vq WddS1swYdS)Nz /proc/net/tcp)openreadlinestrip)r fLr r rentriess  "zProcServerMixin.entriesc Cs&dtttdtdt|dS)N.4Bz=L)joinr"rstructunpackpackr#)r hexstrr r rdottedQuadFromHexStringsz'ProcServerMixin.dottedQuadFromHexStringcCs*|d\}}||}t|d}||fS)N:rW)rr]r#)r packedaddrportr r r unpackAddresss  zProcServerMixin.unpackAddresscCsN|}||d\}}||d\}}t|d}||f||f|fS)Nrr)rQrrbr#)r r*r+ localAddr localPort remoteAddr remotePortrMr r r parseLines  zProcServerMixin.parseLinecCsP|D] }||\}}}||kr$|d|dkr$|j||fSqt)Nr)rTrh SYSTEM_NAMErNr)r rFrGentrdrfrMr r rr5s zProcServerMixin.lookupr )rrrrripwdrJrN ImportErrorrTr]rbrhr5r r r rrHs    rHc@s@eZdZeeeefZddZddZ ddZ ddZ d d Z d S) IdentClientcCs g|_dSr )queriesr r r r__init__s zIdentClient.__init__cCsR|jt||ft|jdkr|jddS|d||f|jddS)zK Lookup user information about the specified address pair. rr%d, %d)rnappendrDeferredr r;)r r,r-r r rr5s zIdentClient.lookupcCsl|js td|dS|jd\}}}||||jr4|d|jdd|jddfdSdS)NzUnexpected server response: rrqrr)rnrmsgpop parseResponser;)r r*d_r r rr.s *zIdentClient.lineReceivedcCs(|jD] }|dt|qg|_dSrK)rnerrbackr)r reasonqr r rconnectionLosts  zIdentClient.connectionLostcCs|dd}t|dkr|t|dSttj|\}}}|dkr@|jD]}|j|kr6|||dSq%|t|dS| ||fdS)Nr^rERROR) rr ryrr"rrQ errorTypesr callback)r deferredr*r+portstypeaddInfoetr r rrvs    zIdentClient.parseResponseN) rrrrrrrrror5r.r|rvr r r rrms   rm)rrrrrrmrH)rrYtwisted.internetrtwisted.protocolsrtwisted.pythonrrr%r& ExceptionrrrrLineOnlyReceiverrrHrm__all__r r r rs      Y2 0