U
    �Acj  ã                   @   sÄ   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	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZG dd„ dƒZG dd„ dejƒZd	ddddddgZdS )z 
Ident protocol implementation.
é    N)Údefer)Úbasic)ÚfailureÚlogé   iÿÿ  c                   @   s"   e Zd ZdZdZedœdd„ZdS )Ú
IdentErrorz;
    Can't determine connection owner; reason unknown.
    zUNKNOWN-ERROR)Úreturnc                 C   s   | j S ©N)ÚidentDescription©Úself© r   ú;/tmp/pip-unpacked-wheel-nrvc_22y/twisted/protocols/ident.pyÚ__str__   s    zIdentError.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   Ústrr   r   r   r   r   r      s   r   c                   @   s   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   c                   @   s   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   c                   @   s   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   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zd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                 C   s¨   |  d¡}t|ƒdkr |  ¡  n„ztt|ƒ\}}W n tk
rN   |  ¡  Y nVX t|  krdtkrŽn n&t|  kr|tkrŽn n|  ||¡ n|  	t
 tƒ ¡||¡ d S )Nú,é   )ÚsplitÚlenÚinvalidQueryÚmapÚintÚ
ValueErrorÚ	_MIN_PORTÚ	_MAX_PORTÚ
validQueryÚ	_ebLookupr   ZFailurer   )r   ÚlineÚpartsÚportOnServerÚportOnClientr   r   r   ÚlineReceivedH   s.    

 ÿÿ
 þþ

  ÿzIdentServer.lineReceivedc                 C   s   | j  ¡  d S r	   )Ú	transportZloseConnectionr   r   r   r   r   \   s    zIdentServer.invalidQueryc                 C   sL   | j  ¡ j|f}| j  ¡ j|f}t | 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)r+   ZgetHostÚhostZgetPeerr   ZmaybeDeferredÚlookupZaddCallbackÚ	_cbLookupZ
addErrbackr%   )r   r(   r)   Z
serverAddrZ
clientAddrr   r   r   r$   _   s      ÿ  þzIdentServer.validQueryc                 C   s"   |\}}|   d||||f ¡ d S )Nz%d, %d : USERID : %s : %s)ÚsendLine)r   ÚresultÚsportÚcportZsysNameZuserIdr   r   r   r.   m   s    zIdentServer._cbLookupc                 C   sJ   |  t¡r"|  d|||jf ¡ n$t |¡ |  d||t|jƒf ¡ d S )Nz%d, %d : ERROR : %s)Úcheckr   r/   Úvaluer   Úerr)r   r   r1   r2   r   r   r   r%   q   s    

ÿzIdentServer._ebLookupc                 C   s
   t ƒ ‚dS )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.
        N©r   )r   ÚserverAddressÚclientAddressr   r   r   r-   z   s    zIdentServer.lookupN)
r   r   r   r   r*   r   r$   r.   r%   r-   r   r   r   r   r   ;   s   	r   c                   @   sz   e Zd ZdZdZzddlmZ efdd„Z[W n ek
rL   ddd„ZY nX d	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚProcServerMixinzDImplements lookup() to grab entries for responses from /proc/net/tcpZLINUXr   )Úgetpwuidc                 C   s   ||ƒd S ©Nr   r   ©r   Úuidr:   r   r   r   ÚgetUsernameœ   s    zProcServerMixin.getUsernameNc                 C   s
   t ƒ ‚d S r	   r6   r<   r   r   r   r>   ¢   s    c              	   c   s4   t dƒ�"}| ¡  |D ]}| ¡ V  qW 5 Q R X d S )Nz/proc/net/tcp)ÚopenÚreadlineÚstrip)r   ÚfÚLr   r   r   Úentries¥   s    
zProcServerMixin.entriesc                 C   s&   d  ttt dt dt|dƒ¡¡ƒ¡S )NÚ.Z4Bz=Lé   )Újoinr   r   ÚstructÚunpackÚpackr    )r   Zhexstrr   r   r   ÚdottedQuadFromHexString«   s    ÿz'ProcServerMixin.dottedQuadFromHexStringc                 C   s*   |  d¡\}}|  |¡}t|dƒ}||fS )Nú:rF   )r   rK   r    )r   ÚpackedÚaddrÚportr   r   r   ÚunpackAddress°   s    

zProcServerMixin.unpackAddressc                 C   sN   |  ¡  ¡ }|  |d ¡\}}|  |d ¡\}}t|d ƒ}||f||f|fS )Nr   r   é   )rA   r   rP   r    )r   r&   r'   Ú	localAddrZ	localPortÚ
remoteAddrZ
remotePortr=   r   r   r   Ú	parseLine¶   s
    zProcServerMixin.parseLinec                 C   sT   |   ¡ D ]@}|  |¡\}}}||kr|d |d kr| j|  |¡f  S qtƒ ‚d S )Nr   )rD   rT   ÚSYSTEM_NAMEr>   r   )r   r7   r8   ÚentrR   rS   r=   r   r   r   r-   ½   s
    zProcServerMixin.lookup)N)r   r   r   r   rU   Úpwdr:   r>   ÚImportErrorrD   rK   rP   rT   r-   r   r   r   r   r9   ”   s   r9   c                   @   s@   e Zd ZeeeefZdd„ Zdd„ Z	dd„ Z
dd„ Zd	d
„ ZdS )ÚIdentClientc                 C   s
   g | _ d S r	   )Úqueriesr   r   r   r   Ú__init__É   s    zIdentClient.__init__c                 C   sR   | j  t ¡ ||f¡ t| j ƒdkr2| j d d S |  d||f ¡ | j d d S )zK
        Lookup user information about the specified address pair.
        r   éÿÿÿÿr   ú%d, %d)rZ   Úappendr   ZDeferredr   r/   )r   r(   r)   r   r   r   r-   Ì   s
    zIdentClient.lookupc                 C   sf   | j st d|›�¡ nJ| j  d¡\}}}|  ||¡ | j rb|  d| j d d | j d d f ¡ d S )NzUnexpected server response: r   r]   r   r   )rZ   r   ÚmsgÚpopÚparseResponser/   )r   r&   ÚdÚ_r   r   r   r*   ×   s    zIdentClient.lineReceivedc                 C   s(   | j D ]}|d  t|ƒ¡ qg | _ d S r;   )rZ   Úerrbackr   )r   ÚreasonÚqr   r   r   ÚconnectionLostà   s    
zIdentClient.connectionLostc                 C   sŽ   |  dd¡}t|ƒdkr(| t|ƒ¡ nbttj|ƒ\}}}|dkr|| jD ]"}|j|krH| ||ƒ¡  d S qH| t|ƒ¡ n| 	||f¡ d S )NrL   r   é   ÚERROR)
r   r   rd   r   r   r   rA   Ú
errorTypesr
   Úcallback)r   Údeferredr&   r'   ZportsÚtypeZaddInfoÚetr   r   r   ra   å   s    

zIdentClient.parseResponseN)r   r   r   r   r   r   r   rj   r[   r-   r*   rg   ra   r   r   r   r   rY   Æ   s   	rY   )r   rH   Ztwisted.internetr   Ztwisted.protocolsr   Ztwisted.pythonr   r   r"   r#   Ú	Exceptionr   r   r   r   ZLineOnlyReceiverr   r9   rY   Ú__all__r   r   r   r   Ú<module>   s*   		Y20ù