
    Ϫf                        d Z ddlZddlmZ ddlmZ ddlmZmZ dZ	dZ
 G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d dej                         Z G d d      Z G d dej                         Zg dZy)z 
Ident protocol implementation.
    N)defer)basic)failurelog   i  c                        e Zd ZdZdZdefdZy)
IdentErrorz;
    Can't determine connection owner; reason unknown.
    zUNKNOWN-ERRORreturnc                     | j                   S N)identDescriptionselfs    9/usr/lib/python3/dist-packages/twisted/protocols/ident.py__str__zIdentError.__str__   s    $$$    N)__name__
__module____qualname____doc__r   strr    r   r   r	   r	      s     '% %r   r	   c                       e Zd ZdZdZy)NoUserz
    The connection specified by the port pair is not currently in use or
    currently not owned by an identifiable entity.
    zNO-USERNr   r   r   r   r   r   r   r   r   r      s    
 !r   r   c                       e Zd ZdZdZy)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-PORTNr   r   r   r   r   r   '   s     &r   r   c                       e Zd ZdZdZy)
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-USERNr   r   r   r   r   r   2   s    
 %r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	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                    |j                  d      }t        |      dk7  r| j                          y 	 t        t        |      \  }}t
        |cxk  r	t        k  r,n n)t
        |cxk  r	t        k  rn n| j                  ||       y | j                  t        j                  t                     ||       y # t        $ r | j                          Y y w xY w)N,   )splitleninvalidQuerymapint	_MIN_PORT	_MAX_PORT
validQuery	_ebLookupr   Failurer   
ValueError)r   linepartsportOnServerportOnClients        r   lineReceivedzIdentServer.lineReceivedH   s    

3u:?-0e_*l
 ::!\>Y>OOL,?NN6l  $!!#$s   B3 3CCc                 8    | j                   j                          y r   )	transportloseConnectionr   s    r   r'   zIdentServer.invalidQuery\   s    %%'r   c                 J   | j                   j                         j                  |f}| j                   j                         j                  |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)r6   getHosthostgetPeerr   maybeDeferredlookupaddCallback	_cbLookup
addErrbackr-   )r   r2   r3   
serverAddr
clientAddrs        r   r,   zIdentServer.validQuery_   s|     ^^++-22L@
^^++-22L@
DKKZ@LLNNL,	

*T^^\<
@r   c                 >    |\  }}| j                  d||||fz         y )Nz%d, %d : USERID : %s : %s)sendLine)r   resultsportcportsysNameuserIds         r   r?   zIdentServer._cbLookupm   s'    "&1UE7F4SSTr   c                      |j                   t              r"| j                  d|||j                  fz         y t	        j
                  |       | j                  d||t        |j                        fz         y )Nz%d, %d : ERROR : %s)checkr	   rD   valuer   err)r   r   rF   rG   s       r   r-   zIdentServer._ebLookupq   s]    7==$MM/5%2OOPGGGMM%z'--7P(QQr   c                     t               )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      r   r=   zIdentServer.lookupz   s    . lr   N)
r   r   r   r   r4   r'   r,   r?   r-   r=   r   r   r   r!   r!   ;   s'    
((AUr   r!   c                   j    e Zd ZdZdZ	 ddlmZ efdZ[d Z	d	 Z
d
 Zd Zd Zy# e$ r ddZY w xY w)ProcServerMixinzDImplements lookup() to grab entries for responses from /proc/net/tcpLINUXr   )getpwuidc                      ||      d   S Nr   r   r   uidrU   s      r   getUsernamezProcServerMixin.getUsername   s    C=##r   Nc                     t               r   rO   rX   s      r   rZ   zProcServerMixin.getUsername   s
    ,r   c              #      K   t        d      5 }|j                          |D ]  }|j                           	 d d d        y # 1 sw Y   y xY ww)Nz/proc/net/tcp)openreadlinestrip)r   fLs      r   entrieszProcServerMixin.entries   sG     /" 	 aJJL  ggi 	  	  	 s   A*A	AAAc                     dj                  t        t        t        j                  dt        j
                  dt        |d                              S )N.4Bz=L   )joinr(   r   structunpackpackr)   )r   hexstrs     r   dottedQuadFromHexStringz'ProcServerMixin.dottedQuadFromHexString   s9    xxV]]4T3vr?)KLM
 	
r   c                 l    |j                  d      \  }}| j                  |      }t        |d      }||fS )N:rf   )r%   rl   r)   )r   packedaddrports       r   unpackAddresszProcServerMixin.unpackAddress   s9    \\#&
d++D14}Tzr   c                     |j                         j                         }| j                  |d         \  }}| j                  |d         \  }}t        |d         }||f||f|fS )Nr   r$      )r_   r%   rr   r)   )r   r0   r1   	localAddr	localPort
remoteAddr
remotePortrY   s           r   	parseLinezProcServerMixin.parseLine   sl    

""$#11%(;	9!%!3!3E!H!=
J%(m9%
J'?DDr   c                     | j                         D ]G  }| j                  |      \  }}}||k(  s|d   |d   k(  s*| j                  | j                  |      fc S  t	               )Nr   )rb   ry   SYSTEM_NAMErZ   r   )r   rP   rQ   entru   rw   rY   s          r   r=   zProcServerMixin.lookup   sk    <<> 	AC)-)<&Iz3]*y|}Q?O/O(($*:*:3*?@@	A
 hr   r   )r   r   r   r   r{   pwdrU   rZ   ImportErrorrb   rl   rr   ry   r=   r   r   r   rS   rS      sO    NK
 ,4 	$  

E;  	s   & 	22rS   c                   6    e Zd ZeeeefZd Zd Z	d Z
d Zd Zy)IdentClientc                     g | _         y r   )queriesr   s    r   __init__zIdentClient.__init__   s	    r   c                    | j                   j                  t        j                         ||f       t	        | j                         dkD  r| j                   d   d   S | j                  d||fz         | j                   d   d   S )zK
        Lookup user information about the specified address pair.
        r   r   %d, %d)r   appendr   Deferredr&   rD   )r   r2   r3   s      r   r=   zIdentClient.lookup   ss     	U^^-|\JKt||q <<#A&&h,!==>||B""r   c                 6   | j                   st        j                  d|       y | j                   j                  d      \  }}}| j	                  ||       | j                   r7| j                  d| j                   d   d   | j                   d   d   fz         y y )NzUnexpected server response: r   r   r   r$   )r   r   msgpopparseResponserD   )r   r0   d_s       r   r4   zIdentClient.lineReceived   s    ||GG24(;<ll&&q)GAq!q$'||h$,,q/!*<dll1oa>P)QQR r   c                 n    | j                   D ]  }|d   j                  t        |             ! g | _         y rW   )r   errbackr	   )r   reasonqs      r   connectionLostzIdentClient.connectionLost   s1     	-AaDLLF+,	-r   c                    |j                  dd      }t        |      dk7  r|j                  t        |             y t	        t
        j                  |      \  }}}|dk(  rT| j                  D ]*  }|j                  |k(  s|j                   ||              y  |j                  t        |             y |j                  ||f       y )Nrn   r$      ERROR)
r%   r&   r   r	   r(   r   r_   
errorTypesr   callback)r   deferredr0   r1   portstypeaddInfoets           r   r   zIdentClient.parseResponse   s    

3"u:?Z-.#&syy%#8 E4w// B**g5 ((D2   D!12!!4/2r   N)r   r   r   r	   r   r   r   r   r   r=   r4   r   r   r   r   r   r   r      s*    fk:>J	#S
3r   r   )r	   r   r   r   r!   r   rS   )r   rh   twisted.internetr   twisted.protocolsr   twisted.pythonr   r   r*   r+   	Exceptionr	   r   r   r   LineOnlyReceiverr!   rS   r   __all__r   r   r   <module>r      s   
  " # '		% %!Z !&* &% %V%(( Vr/ /d,3%(( ,3^r   