
    \d                         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dS )z 
Ident protocol implementation.
    N)defer)basic)failurelog   i  c                   "    e Zd ZdZdZdefdZdS )
IdentErrorz;
    Can't determine connection owner; reason unknown.
    zUNKNOWN-ERRORreturnc                     | j         S N)identDescriptionselfs    7lib/python3.11/site-packages/twisted/protocols/ident.py__str__zIdentError.__str__   s    $$    N)__name__
__module____qualname____doc__r   strr    r   r   r	   r	      sA          '% % % % % % %r   r	   c                       e Zd ZdZdZdS )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dS )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dS )
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                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	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                     |                     d          }t          |          dk    r|                                  d S 	 t          t          |          \  }}t
          |cxk    rt          k    r5n n2t
          |cxk    rt          k    rn n|                     ||           d S |                     t          j
        t                                ||           d S # t          $ r |                                  Y d S 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6l      $ $ $!!######$s    C C=<C=c                 8    | j                                          d S r   )	transportloseConnectionr   s    r   r'   zIdentServer.invalidQuery\   s    %%'''''r   c                 $   | j                                         j        |f}| j                                         j        |f}t	          j        | j        ||                              | j        ||          	                    | j
        ||           dS )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     ^++--2L@
^++--2L@
DKZ@@LLNL,	
 	

*T^\<
@
@
@
@
@r   c                 H    |\  }}|                      d||||fz             d S )Nz%d, %d : USERID : %s : %s)sendLine)r   resultsportcportsysNameuserIds         r   r?   zIdentServer._cbLookupm   s3    "&1UE7F4SSTTTTTr   c                      |j         t                    r"|                     d|||j        fz             d S t	          j        |           |                     d||t          |j                  fz             d S )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PPPPGGMM%z'-7P7P(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    . llr   N)
r   r   r   r   r4   r'   r,   r?   r-   r=   r   r   r   r!   r!   ;   s~        
 
  (( ( (A A AU U U      r   r!   c                   n    e Zd ZdZdZ	 ddlmZ efdZ[n# e$ r ddZY nw xY wd Z	d	 Z
d
 Zd Zd ZdS )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    8C==##r   Nc                     t                      r   rO   rX   s      r   rZ   zProcServerMixin.getUsername   s    ,,r   c              #      K   t          d          5 }|                                 |D ]}|                                V  	 d d d            d S # 1 swxY w Y   d S )Nz/proc/net/tcp)openreadlinestrip)r   fLs      r   entrieszProcServerMixin.entries   s      /"" 	 aJJLLL    ggii 	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   0AAAc                     d                     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   sE    xxV]4T3vr??)K)KLLMM
 
 	
r   c                     |                     d          \  }}|                     |          }t          |d          }||fS )N:rf   )r%   rl   r)   )r   packedaddrports       r   unpackAddresszProcServerMixin.unpackAddress   sA    \\#&&
d++D114}}Tzr   c                    |                                                                 }|                     |d                   \  }}|                     |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   sy    

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

  E E E    s    	##rS   c                   8    e Zd ZeeeefZd Zd Z	d Z
d Zd ZdS )IdentClientc                     g | _         d S r   )queriesr   s    r   __init__zIdentClient.__init__   s    r   c                    | j                             t          j                    ||f           t	          | j                   dk    r| j         d         d         S |                     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   s~     	U^--|\JKKKt|q  <#A&&h,!==>>>|B""r   c                 8   | j         st          j        d|           d S | j                             d          \  }}}|                     ||           | j         r>|                     d| j         d         d         | j         d         d         fz             d S d S )NzUnexpected server response: r   r   r   r$   )r   r   msgpopparseResponserD   )r   r0   d_s       r   r4   zIdentClient.lineReceived   s    | 	SG;4;;<<<<<l&&q))GAq!q$'''| Sh$,q/!*<dl1oa>P)QQRRRRRS Sr   c                 x    | j         D ]*}|d                             t          |                     +g | _         d S rW   )r   errbackr	   )r   reasonqs      r   connectionLostzIdentClient.connectionLost   s?     	- 	-AaDLLF++,,,,r   c                    |                     dd          }t          |          dk    r$|                    t          |                     d S t	          t
          j        |          \  }}}|dk    rZ| j        D ].}|j        |k    r!|                     ||                      d S /|                    t          |                     d S |	                    ||f           d S )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--.....#&sy%#8#8 E4w/  B*g55 ((D222 6   D!1!122222!!4/22222r   N)r   r   r   r	   r   r   r   r   r   r=   r4   r   r   r   r   r   r   r      sp        fk:>J  	# 	# 	#S S S  
3 3 3 3 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 V V V%( V V Vr/ / / / / / / /d-3 -3 -3 -3 -3%( -3 -3 -3`  r   